javascript 延迟加载外部JavaScript文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 延迟加载外部JavaScript文件相关的知识,希望对你有一定的参考价值。

setTimeout(load_js, 4000); // Delay 4 seconds in loading function
function load_js()
{
    // Get the head tag
    var head_ID = document.getElementsByTagName("head")[0]; 
    // Create script element       
    var script_element = document.createElement('script');
    // Set the script type to JavaScript
    script_element.type = 'text/javascript';
    // External JS file
    script_element.src = 'http:/www.domain.com/script.js';
    head_ID.appendChild(script_element);
}

or

setTimeout(function() {
    // Get the head tag
    var head_ID = document.getElementsByTagName("head")[0]; 
    // Create script element       
    var script_element = document.createElement('script');
    // Set the script type to JavaScript
    script_element.type = 'text/javascript';
    // External JS file
    script_element.src = 'http:/www.domain.com/script.js';
    head_ID.appendChild(script_element);
}, 4000);

以上是关于javascript 延迟加载外部JavaScript文件的主要内容,如果未能解决你的问题,请参考以下文章