Old fashioned html onclick return false not working inIE when jQuery script is enabled

Ok, so I'm pretty 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, as shown below, with both href and the with and onclick event. The first link will not follow href, because onclick returns false, and the second link will be caused by the fact that onclick returns true.

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

It works just the hunky dory in every browser, as it should be, the fact is that as soon as I enable the jQuery script on the page, it stops working in all versions of IE, and then always follows href whether onclick returns false or not . (it continues to work perfectly in other browsers)

Now, if I add an event using jquery and call .preventDefault () on the event object instead of doing it in the old fashioned way, it behaves correctly, and you can say that you just do it? But I have a website with thousands of lines of code, and I add jquery support, I don’t want to run the risk that I can skip the already defined html onclick = "" and break the website.

I can’t understand why jQuery should prevent completely normal javascript functions from working, is this a jQuery error or am I missing something?

+4
source share
3 answers

The most likely reason is that you have a syntax error elsewhere on the page that prevents javascript from working on the page. Enable script error notifications in your browser to make sure that they do not hide errors from you.

+3
source

Try also:

onClick = "event.returnValue = false; return false;"

+2
source

I adjusted your sample code for the second channel and tested it in IE 8 and it works fine. I bet JohnFx is right, and you have another error on your page to stop working.

I tested using the built-in jquery library as well.

Example:

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

Source: https://habr.com/ru/post/1303979/


All Articles