2

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?

michael
  • 106,540
  • 116
  • 246
  • 346
  • Dupe: http://stackoverflow.com/questions/809057/how-do-i-programmatically-click-on-an-element-in-firefox Your last concrete question is by the way misleading. I would have answered "for example Internet Explorer", but that's probably not the right solution ;) – BalusC Apr 23 '10 at 21:20
  • How to programatically induce a JS `click` event. – Richard JP Le Guen Apr 23 '10 at 21:20

5 Answers5

1

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    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 Apr 23 '10 at 21:54
  • @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 Apr 23 '10 at 22:04
0

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
  • 658
  • 1
  • 12
  • 30
0

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">fooContent</a>

David
  • 2,785
  • 1
  • 16
  • 8
  • 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 Apr 23 '10 at 21:51
  • 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 Apr 23 '10 at 22:06
0

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>
aceofspades
  • 7,568
  • 1
  • 35
  • 48
-1
$('#suchandsuch').click();

Is this not working for you?

Robert
  • 265
  • 4
  • 14