IE8 - how to run jquery code after content loads?

后端 未结 2 1372
时光说笑
时光说笑 2021-01-07 02:46

I have a situation where I load a parent web page (happens to be Java JSP) which has content I am loading using ajax (asynch) inside of a document ready function expecting t

相关标签:
2条回答
  • 2021-01-07 03:24

    This is a bug that affects IE6 7 and 8. jQuery's document ready handler doesn't fire until right before or right after the window load event in IE 6 7 and 8. This does not happen in IE9.

    One way to fix it is to handle the event yourself.

    <body class="jquerydomready">
        <!--[if lt IE 9]>
        <script>
            $('body').removeClass('jquerydomready');
        </script>
        <![endif]-->
    

    and then in your script use this:

    function init() {
        // code here will be ran when document is ready
        $("body").css("background-color","green");
    }
    
    if ( $("body").is(".jquerydomready") ) {
        $(init); // not oldIE
    }
    else {
        // oldIE way
        document.onreadystatechange = function() {
            if (document.readyState == "interactive") init();    
        }​
    }
    

    Keep in mind though that if you are performing ajax requests and expecting them to happen quickly or else code won't work, i suggest moving to a system that doesn't require them to happen quickly because you can't rely on the network always being quick.

    ticket: http://bugs.jquery.com/ticket/12282

    It currently is not actually marked as a bug, but if you follow the history on this issue it has been fixed and unfixed several times throughout the development of jQuery.

    Edit: I'm not entirely sure on the IE6 part of this answer, i haven't tested IE6 with this.

    Here's a supporting JSFiddle showing that it improperly waits in IE7 and 8 (again not tested in IE6).

    Before the above fix: http://jsfiddle.net/PFWmS/

    After the above fix: http://jsfiddle.net/PFWmS/7

    0 讨论(0)
  • 2021-01-07 03:24

    Have you tried moving your section to the end of the page?

    0 讨论(0)
提交回复
热议问题