开发者

Javascript: Onload event in Firefox not firing

I am making a dynamic script call to load some extenal JS before window's onload event. Here is the sample code.

var temp = document.createElement('script');
temp.type = 'text/javascript';
temp.async = temp.defer= true;
temp.src = "http://stevesouders.com/efws/iframe-empty.php?t==";
window.setTimeout(function(){document.g开发者_StackOverflowetElementsByTagName('head')[0].appendChild(temp)},0);

This code is perfectly working on IE (Window onload event is not waiting for dynamic script resource download). However it's not working on Firefox. Window onload event is waiting for the dynamic script resource download.

Are there any workarounds for this issue. You can use http://nidhisekhar.com/samples/async_script_call_settimeout.html link to see the behavior on IE and Firefox. I appreciate your help.

Thanks, Raja


try this JavaScript function:

// Adds script tag to head of the page
function addScriptToHead(source, code, type) {
    var script = document.createElement('script');
    if (type === 'js') {
        script.setAttribute('type', 'text/javascript');
    }
    if (source !== '') {
        script.setAttribute('src', source);
    }
    if (code !== '') {
        if (document.all && !window.opera)  {
            script.text = code;
        } else {
            script.innerHTML = code;
        }
    }
    document.getElementsByTagName('head')[0].appendChild(script);
}

call example:

addScriptToHead(path, '', 'js');

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜