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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何判斷<script>標(biāo)簽是否加載失敗

如何判斷<script>標(biāo)簽是否加載失敗

慕哥6287543 2019-10-26 12:47:29
我正在向<script>頁(yè)面的中動(dòng)態(tài)添加標(biāo)簽<head>,并且我想知道加載是否以某種方式失敗-404,加載的腳本中的腳本錯(cuò)誤等。在Firefox中,這有效:var script_tag = document.createElement('script');script_tag.setAttribute('type', 'text/javascript');script_tag.setAttribute('src', 'http://fail.org/nonexistant.js');script_tag.onerror = function() { alert("Loading failed!"); }document.getElementsByTagName('head')[0].appendChild(script_tag);但是,這在IE或Safari中不起作用。有誰(shuí)知道在Firefox以外的瀏覽器中實(shí)現(xiàn)此目的的方法嗎?(我認(rèn)為不需要在.js文件中放置特殊代碼的解決方案是一個(gè)很好的解決方案。它既笨拙又不靈活。)
查看完整描述

3 回答

?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊

Erwinus的腳本效果很好,但是編碼卻不太清楚。我自由地清理它并破譯它在做什么。我進(jìn)行了以下更改:


有意義的變量名

使用prototype。

require() 使用參數(shù)變量

alert()默認(rèn)情況下不返回任何消息

修復(fù)了一些語(yǔ)法錯(cuò)誤和我遇到的范圍問(wèn)題

再次感謝Erwinus,該功能本身已經(jīng)存在。


function ScriptLoader() {

}


ScriptLoader.prototype = {


    timer: function (times, // number of times to try

                     delay, // delay per try

                     delayMore, // extra delay per try (additional to delay)

                     test, // called each try, timer stops if this returns true

                     failure, // called on failure

                     result // used internally, shouldn't be passed

            ) {

        var me = this;

        if (times == -1 || times > 0) {

            setTimeout(function () {

                result = (test()) ? 1 : 0;

                me.timer((result) ? 0 : (times > 0) ? --times : times, delay + ((delayMore) ? delayMore : 0), delayMore, test, failure, result);

            }, (result || delay < 0) ? 0.1 : delay);

        } else if (typeof failure == 'function') {

            setTimeout(failure, 1);

        }

    },


    addEvent: function (el, eventName, eventFunc) {

        if (typeof el != 'object') {

            return false;

        }


        if (el.addEventListener) {

            el.addEventListener(eventName, eventFunc, false);

            return true;

        }


        if (el.attachEvent) {

            el.attachEvent("on" + eventName, eventFunc);

            return true;

        }


        return false;

    },


    // add script to dom

    require: function (url, args) {

        var me = this;

        args = args || {};


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

        var headTag = document.getElementsByTagName('head')[0];

        if (!headTag) {

            return false;

        }


        setTimeout(function () {

            var f = (typeof args.success == 'function') ? args.success : function () {

            };

            args.failure = (typeof args.failure == 'function') ? args.failure : function () {

            };

            var fail = function () {

                if (!scriptTag.__es) {

                    scriptTag.__es = true;

                    scriptTag.id = 'failed';

                    args.failure(scriptTag);

                }

            };

            scriptTag.onload = function () {

                scriptTag.id = 'loaded';

                f(scriptTag);

            };

            scriptTag.type = 'text/javascript';

            scriptTag.async = (typeof args.async == 'boolean') ? args.async : false;

            scriptTag.charset = 'utf-8';

            me.__es = false;

            me.addEvent(scriptTag, 'error', fail); // when supported

            // when error event is not supported fall back to timer

            me.timer(15, 1000, 0, function () {

                return (scriptTag.id == 'loaded');

            }, function () {

                if (scriptTag.id != 'loaded') {

                    fail();

                }

            });

            scriptTag.src = url;

            setTimeout(function () {

                try {

                    headTag.appendChild(scriptTag);

                } catch (e) {

                    fail();

                }

            }, 1);

        }, (typeof args.delay == 'number') ? args.delay : 1);

        return true;

    }

};


$(document).ready(function () {

    var loader = new ScriptLoader();

    loader.require('resources/templates.js', {

        async: true, success: function () {

            alert('loaded');

        }, failure: function () {

            alert('NOT loaded');

        }

    });

});


查看完整回答
反對(duì) 回復(fù) 2019-10-26
  • 3 回答
  • 0 關(guān)注
  • 2884 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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