script.readyState是否可信任检测动态脚本加载的结束? [英] Can script.readyState be trusted to detect the end of dynamic script loading?

查看:1071
本文介绍了script.readyState是否可信任检测动态脚本加载的结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用动态脚本加载来减少初始页面加载的持续时间。为了确保脚本定义的功能和对象可以访问,我需要确保脚本已经完全加载。

I use dynamic script loading to reduce the duration of the initial page load. To ensure that the functions and objects defined by a script are accessible, I need to ensure that the script has been fully loaded.

我已经开发了我自己的Javascript库,因此做了相当多的研究,研究如何在不同的图书馆完成。
在与此问题有关的讨论中, LABjs 的作者凯尔·辛普森(Kyle Simpson)表示:

I have developed my own Javascript library to this end, and thus did quite a lot of research on the subject, studying how it's done in different libraries. During a discussion related to this issue, Kyle Simpson, the author of LABjs, stated that:


LABjs(和许多其他装载程序)在所有脚本元素上设置
onload和onreadystatechange
,知道
一些浏览器会触发一个,一些
将触发另一个...

LABjs (and many other loaders) set both "onload" and "onreadystatechange" on all script elements, knowing that some browsers will fire one, and some will fire the other...

你可以找到一个在这篇文章中的jQuery当前版本中的示例,v1.3.2

// Attach handlers for all browsers
script.onload = script.onreadystatechange = function(){
    if ( !done && (!this.readyState ||
    this.readyState == "loaded" || this.readyState == "complete") ) {
        done = true;
        success();
        complete();

        // Handle memory leak in IE
        script.onload = script.onreadystatechange = null;
        head.removeChild( script );
    }
};

这是最先进的技术,但在分析Opera 9.64中的奇怪行为时,我来了得出的结论是,使用这种技术,onload回调起得太早了。

That's the state of the art, but during the analysis of a strange behavior in Opera 9.64, I came to the conclusion that, using this technique, the onload callback got fired too early.

我会发布自己的发现来回答这个问题,并想进一步收集

I will post my own findings in answer to this question, and would like to gather further evidence and feedback from the community.

推荐答案

在Opera中,script.readyState属性不能被信任。例如,在Opera 9.64中脚本运行之前,readyState加载可能被触发。

In Opera, the script.readyState property cannot be trusted. For example, the readyState "loaded" may be fired before the script runs in Opera 9.64.

我执行了同样的测试在Opera 9.64和Opera 10中,具有不同的结果。

I performed the same test in Opera 9.64 and Opera 10, with different results.

在Opera 9.64中,onreadystatechange处理程序在脚本运行之前一次触发两次。在这两种情况下,readyState属性都加载,这意味着该值不能被信任以检测脚本加载结束:

In Opera 9.64, the onreadystatechange handler gets fired twice, once before and once after the script runs. The readyState property is "loaded" in both cases, which means that this value cannot be trusted to detect the end of the script loading:

Fri Dec 18 2009 17:54:43 GMT+0100
Opera/9.64 (Windows NT 5.1; U; en) Presto/2.1.1
Test for script.readyState behavior started
Added script with onreadystatechange handler
readystatechange: loaded
test1.js: Start
test1.js: Start of closure
test1.js: End of closure
readystatechange: loaded

在Opera 10中,onreadystatechange处理程序仍然使用值loaded触发两次,但两次脚本运行后:

In Opera 10, the onreadystatechange handler still gets fired twice with the value "loaded", but both times after the script ran:

Fri Dec 18 2009 18:09:58 GMT+0100
Opera/9.80 (Windows NT 5.1; U; en) Presto/2.2.15 Version/10.10
Test for script.readyState behavior started
Added script with onreadystatechange handler
test1.js: Start
test1.js: Start of closure
test1.js: End of closure
readystatechange: loaded
readystatechange: loaded

这些不同的行为表明,onreadystatechange不是一种可靠的方式来检测在Opera中加载的脚本。由于Opera还支持onload侦听器,应该使用这个其他机制。

These different behaviors indicate that onreadystatechange is not a reliable way to detect the end of a script loading in Opera. Since Opera also supports the onload listener, this other mechanism should be used instead.

根据这些测试的结果,onreadystatechange应该只用于检测脚本的结尾在Internet Explorer中加载,不应在其他浏览器中设置。

Based on the results of these tests, onreadystatechange should only be used to detect the end of script loading in Internet Explorer, and it should not be set in other browsers.

这篇关于script.readyState是否可信任检测动态脚本加载的结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆