javascript動態載入js指令碼幾種方法

來源:互聯網
上載者:User

javascript動態載入js指令碼
1、直接document.write

 代碼如下 複製代碼

<script language="javascript">

    document.write("<script src='test.js'></script>");

</script>

2、動態改變已有script的src屬性

 代碼如下 複製代碼

<script src='' id="s1"></script>

<script language="javascript">

    s1.src="test.js"

</script>

3、動態建立script元素

 代碼如下 複製代碼

function require(url, options){
    var head, node, isOpera;
    isOpera         = typeof opera !== 'undefined' && opera.toString() === '[object Opera]';
    options.success = options.success || function(){};
    options.error   = options.error || function(){};
    options.type    = options.type || 'text/javascript';
    options.charset = options.charset || 'utf-8';
    options.async   = options.async || true;
     
    head            = document.getElementsByTagName("head")[0];
    node            = document.createElement(//javascript/i.test(options.type) ? 'script' : 'style');
    node.type       = options.type;
    node.charset    = options.charset;
    node.async      = options.async;
    node.src        = url;
    if (node.attachEvent && 
        !(node.attachEvent.toString && node.attachEvent.toString().indexOf('[native code') < 0) && 
        !isOpera){
        node.attachEvent('onreadystatechange', function(){
            if(!node.readyState || node.readyState == "loaded" || node.readyState == "complete"){
                options.success();
            }
        });
    }else{
        node.addEventListener('load', options.success, false);
        node.addEventListener('error', options.error, false);
    }
     
    head.appendChild(node);
}

IE不支援載入失敗檢測

 代碼如下 複製代碼

require('http://code.jquery.com/jquery-1.7.3.min.js', {
    success:function(){
        alert('jquery loaded!');
    },
    error:function(){
        alert('jquery load fail!');
    }
});

方法四

 代碼如下 複製代碼

//動態載入指令檔

function AddScript(scriptID,scriptSRC)
{
    if(document.getElementByIdx(scriptID)!=null)
    return;
    var Heads = document.getElementsByTagName_r("HEAD");
    if(Heads.length==0)
    return;
    var oHead = Heads.item(0);
    var oScript= document.createElement("script");
    oScript.id = scriptID;
    oScript.type = "text/javascript";
    //oScript.text = scriptContent;
    oScript.src = scriptSRC;
    oHead.appendChild(oScript);
}

//動態載入指令碼內容

function AddScript(scriptID,scriptContent)
{
    if(document.getElementByIdx(scriptID)!=null)
    return;
    var Heads = document.getElementsByTagName_r("HEAD");
    if(Heads.length==0)
    return;
    var oHead = Heads.item(0);
    var oScript= document.createElement("script");
    oScript.id = scriptID;
    oScript.type = "text/javascript";
    oScript.text = scriptContent;
    oHead.appendChild(oScript);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.