views:

419

answers:

2

Ok, so I'm quite new to jQuery, but found this bizzare problem just now,

If we ignore jquery for a second and consider this scenario, if i have two links like below both with an href and both with and onclick event. The first link will not follow the href because the onclick returns false, and the second link will because the onclick returns true.

<a href="/page.html" onclick="return false;">Dont follow</a>

<a href="/page.html" onclick="return true;">Follow</a>

This works just hunky dory in every browser as it should, the thing is, as soon as i include the jQuery script on the page this stops working in all versions of IE which then always follows the href whether the onclick returns false or not. (it continues to work fine in other browsers)

Now if i add an event using jquery and call .preventDefault() on the event object instead of doing it the old fashioned way this behaves correctly, and you may say, well just do that then? But i have a site with thousands of lines of code and i am adding jquery support, i dont want to run the risk that i might miss an already defined html onclick="" and break the website.

I cant see why jQuery should prevent perfectly normal javascript concepts from working, so is this a jQuery bug or am I missing something?

+2  A: 

The most likely cause is that you have a syntax error elsewhere in the page that is preventing javascript from running properly on the page. Turn on script error notifications in your browser to make sure it isn't hiding errors from you.

JohnFx
That's my guess as well. It works fine for me in IE. Also, I think you meant to have that second link return true, not false in your post. You can even check that out here --> http://jsbin.com/axoqo3.
ryanulit
Thanks guys, you are both right. Turns out i had a completely unrelated document wide onclick event which IE didn't like.
murdoch
A: 

I corrected your sample code for the second link and tested in IE 8 and it works fine. I'm willing to bet JohnFx is right and you have some other error on your page stopping it from working.

I tested using an included jquery library as well.

Sample:

   <html>
 <head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt; 
 </head>
 <body>
   <a href="/page.html" onclick="return false;">Dont follow</a>
   <a href="/page.html" onclick="return true;">Follow</a>
 </body>
    </html>
Chuck