Capturing onload event when dynamically loading .js files?

佐手、 提交于 2020-01-04 08:40:39

问题


Is there a capture the on load event when dynamically adding a script tag with JavaScript in IE?

The code below works in FireFox and Chrome but not in IE.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>

</body>
</html>
<script type="text/javascript" language="javascript">

    var elemScript = document.createElement("script");
    elemScript.onload = function() {
        $("body").html("<div>jQuery Loaded !</div>");
    };
    elemScript.type = "text/javascript";
    elemScript.src = "script/jquery.js";

    document.getElementsByTagName("head")[0].appendChild(elemScript);

</script>

回答1:


Script tags in IE have use the onreadystatechange event instead of onload: http://unixpapa.com/js/dyna.html



来源:https://stackoverflow.com/questions/1917953/capturing-onload-event-when-dynamically-loading-js-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!