In Javascript code, I would like to programmatically cause the browser to follow a link that’s on my page. Simple case:
<a id="foo" href="mailto:somebody@example.com">something</a>
function goToBar() {
$('#foo').trigger('follow');
}
This is hypothetical as it doesn’t actually work. And no, triggering click doesn’t do it.
I am aware of window.location and window.open but these differ from native link-following in some ways that matter to me: a) in the presence of a <base /> element, and b) in the case of mailto URLs. The latter in particular is significant. In Firefox at least, calling window.location.href = "mailto:somebody@example.com" causes the window’s unload handlers to fire, whereas simply clicking a mailto link does not, as far as I can tell.
I’m looking for a way to trigger the browser’s default handling of links, from Javascript code.
Does such a mechanism exist? Toolkit-specific answers also welcome (especially for Gecko).
As far as I know, window.location does exactly what you are looking for, triggering the browser’s default link clicking behavior.
Some browsers notice the protocol before any events are fired or the actual href is changed.
Trying the fiddle demo mentioned below I get the following results:
onbeforeunloadwith the button and linkonbeforeunloadonly for the buttononbeforeunloadSo a good way to prevent the
unloadevent from being fired is by returning false inbeforeunload.