IUnknown
IUnknown

Reputation: 9809

Prevent an anchor from following href

I have an anchor defined as follows -

<a onClick="javascript:isLinked('');" class="..." href="..."><img border="0" alt="" height="8" width="8" src="..."/> <u>Add another</u></a> 

The javascript function -

function isLinked(sId) {
        if(!sId)
        {
            alert(".....");
            return false;
        }
        else
            document.body.style.cursor='wait';                  
    }

Although I do see the alert being triggered on the click of the anchor,the links still follows the target url.
How can i prevent the link from following?

The browser is IE8(if that is relevant)

Upvotes: 1

Views: 104

Answers (2)

Marc Perrin-Pelletier
Marc Perrin-Pelletier

Reputation: 13456

The javascript way to disable a link is to add return false;. In your case, the boolean is determined by your function isLinked. So the answer is :

<a onClick="return isLinked('');" class="..." href="..."><img ...

In case that might be of use to you, there's also a css way of disabling links, as documented here.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You need to return the value returned by the method from onclick handler

onClick="return isLinked('');"

Upvotes: 1

Related Questions