异步加载JSONP

var loadJsonp = function(callback) {
    var jsonpdata;
    var head = document.getElementsByTagName(‘head‘);
    if (head && head.length) {
        head = head[0];
    } else {
        head = document.body;
    }
    var script = document.createElement(‘script‘);
    script.type = "text/javascript";
    var isLoaded = false;
    script.onload = script.onreadystatechange = function() {
        if (isLoaded) {
            return;
        }
        if ((!this.readyState) || this.readyState == "complete"
                || this.readyState == "loaded") {
            isLoaded = true;
            setTimeout(function() {
                callback(jsonpdata);
            });
            script.onload = script.onreadystatechange = null; //防止IE内存泄漏
        }
    }
    dealData = function(data) {
        jsonpdata = data;
    }
    script.src ="json url";
    head.appendChild(script);
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。