1

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)

IUnknown
  • 9,301
  • 15
  • 50
  • 76
  • use `e.preventDefault();` and `return false;` but for that you should have e in event source in your function – user2009750 May 22 '15 at 09:49
  • I think this question is similar to [prevent-anchor-behaviour](http://stackoverflow.com/questions/4387580/prevent-anchor-behaviour) – Neha May 22 '15 at 09:51

2 Answers2

1

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

onClick="return isLinked('');"
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

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.

Community
  • 1
  • 1
Marc Perrin-Pelletier
  • 12,696
  • 8
  • 28
  • 36