Search code examples
javascripthtmlhref

Avoid href on click to open browser


I have this href link in a control that calls a javascript and pass the variable to it:

<a href="<%#XPath("link").ToString()%>" onclick="return getLink(this)">Link</a>

I need to stop it opening the browser. How can I do that?


Solution

  • event.preventDefault(); will prevent the default behaviour of the click on the element.

    So:

    <a href="<%#XPath("link").ToString()%>" onclick="event.preventDefault(); return getLink(this)">Link</a>
    

    Demo: http://jsfiddle.net/AbwUW/