如何为ie设置onload事件

how to set onload event for ie

本文关键字:onload 事件 设置 ie      更新时间:2023-09-26

我试图在我的文档中插入另一个javascript
我有一个代码:


    script = document.createElement('script');
    script.type = 'text/javascript';
    script.onreadystatechange = function () {
        if (this.readyState == 'complete') proceed();
    } //this for IE but it doesn't work even for IE8
    script.onload = proceed; //this is for other browsers

那么,我怎样才能使它工作在IE

我相信你的代码中缺少了一些东西。也就是将创建的脚本标签附加到文档中。

类似:

var head = document.getElementsByTagName('head')[0];
head.appendChild(script);

在你的脚本行之后。

您可以在http://ikanobori.jp/examples/loading-external-javascript/找到这个脚本,其中http://ikanobori.jp/examples/loading-external-javascript/one.js是第一个文件,http://ikanobori.jp/examples/loading-external-javascript/two.js是动态加载的文件。

我已经验证了这在Internet Explorer 8中工作,但没有更低的版本可用。

你可以试试这个,看看你是否能得到一个完整的

script.onreadystatechange = function () {
    document.title=this.readyState + ' - ' + new Date();
    if (this.readyState == 'complete') start();
}