第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在JS內(nèi)動態(tài)加載JS

在JS內(nèi)動態(tài)加載JS

眼眸繁星 2019-07-02 16:10:51
在JS內(nèi)動態(tài)加載JS我有一個動態(tài)網(wǎng)頁,需要導(dǎo)入外部JS文件(在IF條件)在另一個javascript文件中。我試圖尋找一個可行的解決方案,但沒有奏效。我嘗試使用以下方法將JS文件加載到DOM中document.createElement()但也沒用。顯然,Js被加載到DOM中,但在當(dāng)前的JS文件中無法訪問。jquery中的解決方案也很好。
查看完整描述

3 回答

?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個贊

jQuery$.getScript()有時是有問題的,所以我使用我自己的實(shí)現(xiàn),比如:

jQuery.loadScript = function (url, callback) {
    jQuery.ajax({
        url: url,
        dataType: 'script',
        success: callback,
        async: true
    });}

并把它當(dāng)作:

if (typeof someObject == 'undefined') $.loadScript('url_to_someScript.js', function(){
    //Stuff to do after someScript has loaded});


查看完整回答
反對 回復(fù) 2019-07-02
?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個贊

我的猜測是,在只使用DOM的解決方案中,您所做的事情如下:

var script = document.createElement('script');script.src = something;//do stuff with the script

首先,因?yàn)槟_本沒有添加到文檔樹中,所以不會被加載,所以這是行不通的。此外,即使您這樣做,在加載其他腳本時,javascript的執(zhí)行仍在繼續(xù),因此在完全加載該腳本之前,它的內(nèi)容是不可用的。

你可以聽劇本的load事件,并按照您的意愿對結(jié)果進(jìn)行處理。因此:

var script = document.createElement('script');script.onload = function () {
    //do stuff with the script};script.src = something;document.head.appendChild(script); //or something of the likes


查看完整回答
反對 回復(fù) 2019-07-02
?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個贊

我需要經(jīng)常這樣做,所以我用這個:

var loadJS = function(url, implementationCode, location){
    //url is URL of external file, implementationCode is the code
    //to be called from the file, location is the location to 
    //insert the <script> element

    var scriptTag = document.createElement('script');
    scriptTag.src = url;

    scriptTag.onload = implementationCode;
    scriptTag.onreadystatechange = implementationCode;

    location.appendChild(scriptTag);};var yourCodeToBeCalled = function()
    {//your code goes here}loadJS('yourcode.js', yourCodeToBeCalled, document.body);

有關(guān)更多信息,請參見本網(wǎng)站如何在另一個JavaScript文件中包含JavaScript文件?,這是我的功能理念的來源。


查看完整回答
反對 回復(fù) 2019-07-02
  • 3 回答
  • 0 關(guān)注
  • 461 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號