Javascript Internet Explorer 中“script”标签的“onload”处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4845762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:22:23  来源:igfitidea点击:

'onload' handler for 'script' tag in internet explorer

javascriptinternet-exploreronload

提问by Nikita Rybak

I've been using this functionto attach onload handler to a script tag, it seems to be the recommended way over the internet.
Yet, it doesn't work in internet explorer, if page is loaded already (tested in ie 8). You can see that it works in normal browsers (fires alert when script is loaded).

我一直在使用此函数将 onload 处理程序附加到脚本标记,这似乎是 Internet 上推荐的方法。
然而,如果页面已经加载(在 ie 8 中测试),它在 Internet Explorer 中不起作用。您可以看到它在普通浏览器中工作(加载脚本时触发警报)。

Am I missing something?
Thank you

我错过了什么吗?
谢谢

回答by SLaks

You should call jQuery.getScript, which does exactly what you're looking for.

您应该致电jQuery.getScript,这正是您正在寻找的。

EDIT: Here is the relevant source code from jQuery:

编辑:这是来自 jQuery 的相关源代码:

var head = document.getElementsByTagName("head")[0] || document.documentElement;
var script = document.createElement("script");
if ( s.scriptCharset ) {
    script.charset = s.scriptCharset;
}
script.src = s.url;

// Handle Script loading
    var done = false;

// Attach handlers for all browsers
script.onload = script.onreadystatechange = function() {
    if ( !done && (!this.readyState ||
            this.readyState === "loaded" || this.readyState === "complete") ) {
        done = true;
        jQuery.handleSuccess( s, xhr, status, data );
        jQuery.handleComplete( s, xhr, status, data );

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

// Use insertBefore instead of appendChild  to circumvent an IE6 bug.
// This arises when a base node is used (#2709 and #4378).
head.insertBefore( script, head.firstChild );

回答by ngChris

I also had issues with script.onload = runFunction; in IE8.

我也遇到了 script.onload = runFunction; 的问题。在 IE8 中。

I tried jQuery.getScript and it worked perfectly for my needs. The only downside is having to wait for jQuery to be loaded before adding in the script.

我尝试了 jQuery.getScript,它非常适合我的需求。唯一的缺点是在添加脚本之前必须等待 jQuery 加载完毕。

However, since my callback functions utilize jQuery very heavily anyway I find this an extremely acceptable and very minor downside since it creates a very easy to use, cross-browser solution.

然而,由于我的回调函数非常大量地使用 jQuery,我发现这是一个非常可接受且非常小的缺点,因为它创建了一个非常易于使用的跨浏览器解决方案。

Update:

更新:

Here is a way of doing it without using jQuery:

这是一种不使用 jQuery 的方法:

(a modified solution from: https://stackoverflow.com/a/13031185/1339954)

(修改后的解决方案来自:https: //stackoverflow.com/a/13031185/1339954

var url = 'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js';
var headID = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.type='text/javascript';
script.src=url;

//for nonIE browsers
script.onload=function(){
        addVideo();
    }

 //for IE Browsers
 ieLoadBugFix(script, function(){
     addVideo();}
 );

function ieLoadBugFix(scriptElement, callback){
        if (scriptElement.readyState=='loaded' || scriptElement.readyState=='completed') {
             callback();
         }else {
             setTimeout(function() {ieLoadBugFix(scriptElement, callback); }, 100);
         }


 }

headID.appendChild(script);