How to disable page jump while clicking on anchore


Sometimes we want to perform some javascript actions using link but don’t want to jump to another page. We can do this using event object.

For example we have a link say:

Say Hello

Clicking this link will show an alert saying “Hello” but will then move to

To prevent this behavior first we will capture the eventobject and use its method preventDefault():

Say Hello

The above will work fine in Firefox, Chrome, Safari and Opera but does not work in IE instead IE provides a property of event object named returnValue, setting this to false resolves the issue:

Say Hello

Again this works fine with IE, Chrome, Safari, Opera but does not work with Firefox. So the right way is to take care of both i.e. IE and Firefox :

<a href="" onclick="alert('Hello');if(event.preventDefault) event.preventDefault(); else event.returnValue = false;" >Say Hello</a>

or Simply

Say Hello

1 thought on “How to disable page jump while clicking on anchore”

Leave a Reply

Your email address will not be published. Required fields are marked *