I came across a curiosity in jQuery: if I call .click()
on a link the click
event handlers are called, but the link isn't actually followed (as if it were clicked in the browser):
<a id="link" href="http://www.google.com>Link</a>
$("#link").click() // won't take me to Google
But in plain Javascript, everything behaves as expected:
document.getElementById("link").click() // *will* take me to Google
This is apparently intentional behaviour - but I'm struggling to work out why click
was implemented like this - with a special exception for links?
Fiddle here: http://jsfiddle.net/9a6sp/
To clarify: I'm not asking how to click link in JS, but rather, why the default behaviour in jQuery is effectively that links aren't clicked when you call .click()
domelement.click()
isn't supported cross browser for redirecting. If you need to redirect to the location in a link you can use:
window.location = $('#link').prop('href');