首页 > 代码库 > 异步加载JSONP

异步加载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);}

 

异步加载JSONP