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?
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?
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).
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.
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>
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>
$('#suchandsuch').click();
Is this not working for you?