简体   繁体   中英

JQuery .load() not working in Internet Explorer

I am using following code for my website:

<script src="js/jquery-1.10.1.js" type="text/javascript"></script>
<script src="js/jquery-migrate-1.2.1.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $("img#logo").load(function() {
            alert('Hello');
        });
    });
</script>

And this is not working in IE but works fine in Firefox, Chrome and Safari.

I can confirm that your code does work in IE 8 on windows 7 64 using unminified version:

<!DOCTYPE html>
<html>
 <head>
<title>test</title>
     <script type="text/javascript" src="jquery-1.10.1.js"></script>
     <script>
         $(document).ready(function () {
             console.log($.fn.jquery);
             $("img#logo").load(function () {
                 console.log('Hello');
             });
         });
     </script>
</head> 
 <body> 
     <img id="logo" src="somegig.gif" onload="console.log('load');"/>
 </body>
</html>

This will log 1.10.1 then load and then Hello, maybe you have to validate your html and make sure your html is valid maybe that's a problem.

a quick review of http://api.jquery.com/load-event/ provides some caveats:

Caveats of the load event when used with images

A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:

  • It doesn't work consistently nor reliably cross-browser
  • It doesn't fire correctly in WebKit if the image src is set to the same src as before
  • It doesn't correctly bubble up the DOM tree
  • Can cease to fire for images that already live in the browser's cache

Note the first and fourth caveats. Clear the cache and try again.

Also, do you need the jQuery migrate ? Lose it and see if it is glitching your IE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM