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

 

posted @ 2014-11-05 15:28  悄悄靠近你  阅读(479)  评论(0编辑  收藏  举报