$("a").on("click", function (e) {
doSomething();
});
...
<a href="http://website.com">My Link</a>
Will doSomething()
always run before the "href", in every browser?
Join Stack Overflow to learn, share knowledge, and build your career.
$("a").on("click", function (e) {
doSomething();
});
...
<a href="http://website.com">My Link</a>
Will doSomething()
always run before the "href", in every browser?
Yes, your handler will run always first. That's what allows you, for instance, to cancel default behavior (navigate to href url) if necessary
$("a").on("click", function (e) {
e.preventDefault(); // --> if this handle didn't run first, this wouldn't work
doSomething();
});
e.preventDefault()
came at the very end of the handler, after LOTS of code, it would still cancel the href fire?
– D.Tate
Apr 25 '13 at 16:26
Yes it does. If you don't want the href to fire you can call e.preventDefault();
and the browser won't follow the link.
Yes, but only the part that runs before the first await
:
$("a").on("click", async function(e) {
example(); // this will always run
await Promise.resolve();
example3(); // this may not run, as it is after the first `await`, and it is after nagivation
e.preventDefault(); // this does not work, as it is after the first `await`
});