最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Dynamic Script Loading IE problems - Stack Overflow

programmeradmin1浏览0评论

I'm trying to load dynamically script with this code:

var headID = document.getElementsByTagName("head")[0]; 
var script = document.createElement('script'); 
script.type='text/javascript'; 
script.src="js/ordini/ImmOrd.js"; 
script.setAttribute("onload", "crtGridRicProd();");                       
headID.appendChild(script);

I need to launch crtGridRicPrdo() function when the page starts, and in FireFox all works fine but in Internet Explorer I have a problems!

I'm trying to load dynamically script with this code:

var headID = document.getElementsByTagName("head")[0]; 
var script = document.createElement('script'); 
script.type='text/javascript'; 
script.src="js/ordini/ImmOrd.js"; 
script.setAttribute("onload", "crtGridRicProd();");                       
headID.appendChild(script);

I need to launch crtGridRicPrdo() function when the page starts, and in FireFox all works fine but in Internet Explorer I have a problems!

Share Improve this question edited Jul 4, 2011 at 8:18 Jonathan Hall 79.6k19 gold badges158 silver badges201 bronze badges asked Jul 4, 2011 at 8:12 MarcoMarco 411 gold badge1 silver badge3 bronze badges 3
  • Wich version of IE did you use? – Reporter Commented Jul 4, 2011 at 8:15
  • What problems you have with IE ? Is it an error message? Need more details. – Jayantha Lal Sirisena Commented Jul 4, 2011 at 8:16
  • IE 7 and no show error but not load function! – Marco Commented Jul 4, 2011 at 9:21
Add a comment  | 

5 Answers 5

Reset to default 8

Internet explorer does not support "onload" on script tags, instead it offers the "onreadystatechange" (similarly to an xhr object). You can check its state in this way:

script.onreadystatechange = function () {
   if (this.readyState == 'complete' || this.readyState == 'loaded') {
     crtGridRicProd();
   }
};

otherwise you can call crtGridRicProd() at the end of your js file

EDIT

example:

test.js:

function test() {
    alert("hello world");
};

index.html:

<!DOCTYPE html>
<html>

  <head>
    <title>test</title>
</head>
<body>
    <script>
        var head = document.getElementsByTagName("head")[0] || document.body;
        var script = document.createElement("script");

        script.src = "test.js";

        script.onreadystatechange = function () {
            if (this.readyState == 'complete' || this.readyState == 'loaded') {
                test();
            }
        };

        script.onload = function() {
            test();
        };

        head.appendChild(script);

    </script>
</body>

you will see the alert in both browser!

I use the following to load scripts one after another (async=false):

var loadScript = function(scriptUrl, afterCallback) {
            var firstScriptElement = document.getElementsByTagName('script')[0]; 
    var scriptElement = document.createElement('script');
            scriptElement.type = 'text/javascript';
            scriptElement.async = false;
            scriptElement.src = scriptUrl;

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

    if ( typeof afterCallback === "function" ) {
        if ( typeof scriptElement.addEventListener !== "undefined" ) {
            scriptElement.addEventListener("load", afterCallback, false)
        } else {
            scriptElement.onreadystatechange = function(){
                scriptElement.onreadystatechange = null;
                ieLoadBugFix(scriptElement, afterCallback);
            }
        }
    }
    firstScriptElement.parentNode.insertBefore(scriptElement, firstScriptElement);
}

Use it like this:

loadScript('url/to/the/first/script.js', function() {
    loadScript('url/to/the/second/script.js', function() {
    // after both scripts are loaded
    });
});

One bugfix which the script includes is the latency bug for IE.

You are loading script from external source. So you need to wait until it loads. You can call your function after id completed.

var headID = document.getElementsByTagName("head")[0]; 
var script = document.createElement('script'); script.type='text/javascript'; 
script.onload=scriptLoaded;
script.src="js/ordini/ImmOrd.js"; script.setAttribute("onload", "crtGridRicProd();");
headID.appendChild(script);

function scriptLoaded(){
// do your work here
}

When I red your code, I figured out that you try to append an onload event to the script tag.

<html>
 <head>
  <script type="text/javascript" onLoad="crtGridRicPrdo()">
   ...
  </script>
 </head>
 <body>
  ...
 </body>
</html>

This will be the result of your javascript code. Why don't you add it to the body tag? This is the classic way and will defnatly work under IE too. This will also reduce your code:

var bodyID = document.getElementsByTagName("body")[0];
bodyID.setAttribute("onload", "crtGridRicProd();");

For proberly dynamic loading a js-script (or css-file) in IE you must carefully check the path to the loaded file! The path should start from '/' or './'.

Be aware, that IE sometimes loses leading slash - as for instance is described here: https://olgastattest.blogspot.com/2017/08/javascript-ie.html

发布评论

评论列表(0)

  1. 暂无评论