Question

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?

Was it helpful?

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/

OTHER TIPS

If you want to prevent default browser behavior - just return false at onclick

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

or make function getLink to return false

Examples

Try this:

<a href="javascript:void(0)" onclick="return getLink(this)">Link</a>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top