Diety Dieticheskie-produkty Protsedury Sredstva Uprajneniya Fitnes Gimnastika Yoga Pitanie Tsellyulit Zdorove Krasota

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 http://www.example.com/.

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="http://www.example.com/" onclick="alert('Hello');if(event.preventDefault) event.preventDefault(); else event.returnValue = false;" >Say Hello</a>

or Simply

Say Hello
Tagged with:
 

One Response to How to disable page jump while clicking on anchore

  1. Sumit Sood says:

    There is one more option which work in every browser:

    Say Hello

    Cheers
    Enjoy Coding!!!

Leave a Reply

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