I have an anchor with both HREF
and ONCLICK
attributes set. If clicked and Javascript is enabled, I want it to only execute ONCLICK
and ignore HREF
. Likewise, if Javascript is disabled or unsupported, I want it to follow the HREF
URL and ignore ONCLICK
. Below is an example of what I'm doing, which would execute the JS and follow the link concurrently (usually the JS is executed and then the page changes):
<A HREF="http://example.com/no-js-login" ONCLICK="yes_js_login()">Log in</A>
what's the best way to do this?
I'm hoping for a Javascript answer, but I'll accept any method as long as it works, especially if this can be done with PHP.
I've read "a href link executes and redirects page before javascript onclick function is able to finish" already, but it only delays HREF
, but doesn't completely disable it. I'm also looking for something much simpler.
yes_js_login = function() {
// Your code here
return false;
}
If you return false it should prevent the default action (going to the href).
Edit: Sorry that doesn't seem to work, you can do the following instead:
<a href="http://example.com/no-js-login" onclick="yes_js_login(); return false;">Link</a>