tags:

views:

144

answers:

5

Hi,

Is there anyway in JavaScript which emulate user clicks an anchor?

Mozilla(Firefox ) does not implement that. https://developer.mozilla.org/en/DOM/element.click

But is there any browser which does?

A: 
$('#suchandsuch').click();

Is this not working for you?

Robert
He asked about Javascript, and did not mention JQuery.
Mark S
My mistake. However, why not use jQuery (or any other javascript library)? No need to recreate the wheel over and over.
Robert
This will not navigate the link.
SLaks
How can JQuery implements it when browser (Gecko) does not support it?https://developer.mozilla.org/en/DOM/element.click
michael
No, it doesn't. It just fires all functions attached to the `click` event/attribute.
BalusC
A: 

I don't know if any browser would support that behavior, but you could do a "setTimeOut", given a number of seconds you can run a function that will trigger whatever it is that would or could have a click event to it.

However, something needs to trigger the initial function, even if it is an "onLoad" event.

jnkrois
+1  A: 

Is there anyway in JavaScript which emulate user clicks an anchor?

The click() function is not supposed to navigate to the href of the link. The jQuery's one also doesn't. The click() function will however fire all functions attached to the click event / onclick attribute. To change the window location to be the link href's one, just do

window.location = linkElement.href;

You can even go a step further:

<script>
    function navigate(link) {
        window.location = link.href;
    }
</script>

<p><a id="link" href="http://google.com" onclick="navigate(this)">link</a>

<script>
    document.getElementById("link").click();
</script>

But is there any browser which does?

MSIE does it (incorrectly).

BalusC
This is about as good as you can get. Of course it does not and cannot replicate any browser-specific behaviours like shift-click-to-open-in-new-window, ctrl-click-for-new-tab etc. So use scripted links very sparingly; real links that behave normally are almost always preferable.
bobince
@bob: That has indeed to be taken into account. Best would be to forget the whole `onclick` for navigation and just do the `window.location = linkElement.href` in your script.
BalusC
A: 

I recommend to bind events with jquery, then later programmatically click them also with jquery. However, if you just can't include jquery:

function AnchorClick(anchorId) 
{     
    var elem = document.getElementById(anchorId); 
    if (elem.onclick!=null)
    {
        elem.onclick();
    }
    else
    if ((elem.href!=null) && (elem.href!=""))
    {            
        var matches = /^javascript:(.+)$/.exec(elem.href); 
        if ((matches!=null) && (matches.length>1)) 
        { 
            // the href is a javascript snippet, execute it                                           
            var tempUndefined;
            var script = "({func:function(){"+matches[1]+"}})";                                
            var json = eval(script);
            elem.tempFunc = json.func; // set the eval'd function on the item itself so it will be executed with the correct scope
            elem.tempFunc();
            elem.tempFunc = tempUndefined; // eliminate the temp function
        } 
        else 
        { 
            // the href wasnt a javascript snippet, just navigate to the href 
            window.location = elem.href;  
        }
    }        
}

EDITED: This above function was updated to now preserve the scope for any href="javascript:" script handler and supports any of the following anchor formats:

  • anchor with regular script onclick="" attribute. Note the href="javascript:void(0)" is so the link will be rendered as focusable so that in addition to using the mouse, disabled users can use the tab key to navigate to and hit ENTER to cause the click action:

    <a href="javascript:void(0)" onclick="dostuff();">fooContent</a>

  • anchor with javascript href

    <a href="javascript:dostuff();">fooContent</a>

  • anchor with normal navigation link

    <a href="http://www.yahoo.com"&gt;fooContent&lt;/a&gt;

David
Not sure what the `javascript:` matching here is for. It would work fine if just written to `window.location` like any URL; `eval`​ing it inside the function executes it in the wrong scope. Of course no-one should ever actually use a `javascript:` URL, but still...
bobince
Anyone who wants to support putting their clickable objects in the taborder for people who can't use a mouse must use anchors with the href="" attribute set properly. If you dont want to postback the page, but instead do some javascript action you need to use "javascript:" in the href. Non form elements with onclick="" and anchors with no href wont be in the taborder to click by the user, thus disabled people wont be able to click them. It is pretty standard to use javascript: if you aren't using jquery. Using jquery to bind the custom events is recommended over using javascript: in the href.
David
A: 

Try:

window.location = document.getElementById('myAnchorId').href;

This will cause the page to navigate to the href.

<a href="myAnchorId" href="www.mysite.com">Test</a>
fullware