IE8 - 如何将内容加载后运行的jQuery code?如何将、加载、内容、jQuery

2023-09-10 13:55:12 作者:骑着奥巴马去打小日本

我有一个情况我打开一个父网页(恰好是Java的JSP),其中有内容,我使用AJAX(异步)的文件准备函数内部期待的网页渲染不管它可以快速地再运行加载jQuery的code显示页面后执行异步工作。

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 the page to render whatever it can quickly then run the jquery code to perform asynch work after the page is displayed.

它工作得如预期在firefox - 页面呈现快速,然后jQuery的code运行Ajax调用加载其他元素

It works just as expected in firefox - page renders quickly and then the jquery code runs ajax calls loading other elements.

在IE8中等待,直到一切都完成后,再渲染完成的结果。它不接受我的文件准备好尝试。在运行我的code在文档准备部分,然后终于一切的完成结果是屁股上的浏览器,它坐在那里加载页面。

In IE8 it waits until everything is done, then renders the completed results. It does not honor my document ready attempt. It sits there loading the page while running my code in the document ready section, then finally the completed results of everything is plopped on the browser.

有没有什么解决方法吗?什么办法让IE8表现如Firefox在这方面......尽快渲染页面,然后在渲染之后运行一些jQuery code / Ajax调用?

Is there any workaround? Any way to get IE8 to behave like Firefox in this regard ... render the page ASAP and then run some jquery code/ajax calls AFTER rendering?

推荐答案

这是一个影响IE6 7和bug 8. jQuery的文档准备好处理程序不火,直到右前或右在IE 6 7的窗口load事件之后和8这不会发生在IE9。

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();    
    }​
}

但请记住,如果您正在执行Ajax请求,并希望他们能够迅速发生,否则code将无法正常工作,我建议迁移到一个系统,不要求他们迅速发生,因为你可以'牛逼依赖网络总是被很快。

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.

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

目前,它实际上不标记为错误,但如果你遵循这一问题已得到修复和不固定的几次历史贯穿jQuery的开发。

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.

编辑:我不能完全肯定这个答案的IE6一部分,我还没有测试IE6本

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

下面是一个支持的jsfiddle显示它不当等待在IE7和8(又不是在IE6测试)。

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

以上修正前: http://jsfiddle.net/PFWmS/

上面的修复程序后: http://jsfiddle.net/PFWmS/7