I'm trying to get the following code to work in all versions of IE, as it works find in other browsers:
<a href= class=specificClass>Click Me </a>
//Javascript
$(.specificClass).click(
function(e) {
e.preventDefault();
// Do Something
//return false; Doesn't work either
}
);
Switching to href=#
makes it try to go to the top of the page, again only in IE9. For example:
Leaving href=
redirects to the current link itself in IE9.
<a href=# onclick=doSomething(this); return false;> Click Me Two </a>
It seems like both approaches triggers the onclick Javascript to be called, but the default behavior for href=
is not getting overridden. If I use event.preventDefault()
nothing happens.
The below approach works:
<a href=javascript:doSomething(this);> Click Me Two </a>
function doSomething(me) {
// event.preventDefault is not needed as the javascript is added via href
}
However I don't want to have href=javascript: or onclick =doSomethingfor all my anchor tags just to get it to work in IE9.
I also don't want to use a different tag (tried the span tag for example) since it is tricky to style up in all browsers.
Any other ideas?
Looks like it is a legit bug, I have submitted a request to fix it. I have also put in a workaround for now:
https://connect.microsoft.com/IE/feedback/details/812247/event-preventdefault-or-return-false-dont-work-in-ie9