码迷,mamicode.com
首页 > Web开发 > 详细

异步加载JSONP

时间:2014-11-05 16:44:00      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   java   sp   div   on   

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

标签:style   blog   io   color   ar   java   sp   div   on   

原文地址:http://www.cnblogs.com/suyuji/p/4076518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!