Internet Explorer中“script”标记的“onload”处理程序 [英] 'onload' handler for 'script' tag in internet explorer

查看:337
本文介绍了Internet Explorer中“script”标记的“onload”处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用此功能将onload处理程序附加到脚本标记,它似乎是推荐的方式互联网。

然而,如果页面已经加载(在8中测试),它在Internet Explorer中不起作用。您可以看到它在普通浏览器中有效(加载脚本时会触发警报)。

I've been using this function to 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).

我错过了什么吗?

谢谢

Am I missing something?
Thank you

推荐答案

您应该致电 jQuery.getScript ,它完全符合您的要求。

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

编辑:以下是jQuery的相关源代码:

EDIT: Here is the relevant source code from 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 );

这篇关于Internet Explorer中“script”标记的“onload”处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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