JQuery not working on IE

I have the following jQuery script that is actually ignored by Internet Explorer (7 and 8). It works fine in FF and Chrome.

<script type="text/javascript" language="javascript"> $("body").addClass("newclass"); </script> 

It is very simple, but I do not know why IE ignores it. Be aware that code is loaded as dynamic content using JAVA (which should not be a problem, as the rest of the scripts work). I tried calling tha script as a function in an external file, but nothing happens. Can someone help me figure out where my mistake is? Or help me understand IE?

+4
source share
3 answers

Or perhaps in a โ€œfinishedโ€ event a document would be better

 $(document).ready(function() { $('body').addClass('newclass'); }); 
+8
source

I am sure you will have problems with Firefox and Chrome if this is a problem, but try

 $(function () { $('body').addClass('newclass'); }); 

to make sure it is called after loading the document. Perhaps IE needs this.

0
source

Wrap it in $(document).ready(function(){ ... });

Thus, jQuery will start it only after the page is fully loaded.

If you do not, the code will be executed as soon as possible, which may be before loading the DOM, so you may not have a body element to add a class.

The fact that some browsers work, while others do not imply that different browsers (a) load the page at different speeds and / or (b) perform the initial loading tasks in a different order. But you should not worry about it. Just call $.ready() and it will all be done in the correct order for jQuery for you.

0
source

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


All Articles