Search code examples
javascripthtmlhyperlinkunobtrusive-javascriptgraceful-degradation

Unobtrusive JavaScript in links


I want a link to either execute a JavaScript function (if JavaScript is available) or enter the URL in the href attribute if it's not.

<a href="http://www.example.com" onclick="example(); return false;">

In this example, I'd want to execute example() and not enter the link if JavaScript is available or go to http://www.example.com if JavaScript is unavailable.

While this works in almost every browser I've tried it with, in IE 7, regardless of if JavaScript is active or not, it just follows the link. What can I do to prevent this?


Solution

  • This may be what you are looking for

    event.returnValue = false;

    Retrieved from here: event.preventDefault() function not working in IE