script通过script标签跨域加载数据

  1 /**********************************************************
  2 
  3     说明:跨域请求数据Javascript组件
  4 ----------------------------------------------------------
  5     add by OceanHo 2015-4-13 18:18:14
  6 **********************************************************/
  7 ; (function (window, undefined) {
  8 
  9     var _ap = function () {
 10     }, _asyncID = 2048, _asyncType = "asynctype", _version = "1.5.0", _cbKey = "cb", _cb = "";
 11 
 12 
 13     function addSc(url, cbAndParam) {
 14         var script = createSc();
 15         script.setAttribute("src", url + "?" + cbAndParam);
 16         window.document.getElementsByTagName("head")[0].appendChild(script);
 17     };
 18 
 19     function delSc(id) {
 20         var scripts = getSc();
 21         for (var i = 0; i < scripts.length; i++) {
 22             if (scripts[i].attributes["id"]) {
 23                 if (parseInt(scripts[i].attributes["id"].value) === parseInt(id)) {
 24                     remove(scripts[i]);
 25                 }
 26             }
 27         }
 28     };
 29 
 30     function remove(script) {
 31         if ((script) && (script.parentNode)) {
 32             script.parentNode.removeChild(script);
 33         }
 34     };
 35 
 36     function clearSc() {
 37         var scripts = getSc();
 38         for (var i = 0; i < scripts.length; i++) {
 39             remove(scripts[i]);
 40         }
 41     };
 42 
 43     function getSc() {
 44         var s1 = [];
 45         var scripts = window.document.getElementsByTagName("script");
 46         for (var i = 0; i < scripts.length; i++) {
 47             if (scripts[i].attributes["async"]) {
 48                 if (scripts[i].attributes["async"].value === _asyncType) {
 49                     s1.push(scripts[i]);
 50                 }
 51             }
 52         }
 53         return s1;
 54     };
 55 
 56     function createSc() {
 57 
 58         _asyncID++;
 59         var script = window.document.createElement("script");
 60         script.setAttribute("id", _asyncID);
 61         script.setAttribute("async", _asyncType);
 62         script.setAttribute("type", "text/javascript");
 63 
 64         script.onload = script.onreadystatechange = function () {
 65 
 66             if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
 67 
 68                 delSc(this.attributes["id"].value);
 69 
 70                 script.onload = script.onreadystatechange = null;
 71             }
 72         }
 73         return script;
 74     }
 75 
 76     function waitDel(id, interval) {
 77         window.setInterval(delSc(id), interval);
 78     };
 79 
 80     _ap.fn = _ap.prototype = {
 81         constructor: _ap,
 82         version: _version,
 83         init: function (arg) {
 84             return new _ap().constructor;
 85         },
 86 
 87         get: function (url, cbk, cb, param) {
 88             if (!cb) cb = _cb;
 89             if (!cbk) cbk = _cbKey;
 90             var qs = this.map(',', param);
 91             addSc(url, _cbKey + "=" + cb + "&" + qs);
 92         },
 93 
 94         getEasy: function (url, cb, param) {
 95             if (!cb) cb = _cb;
 96             var qs = this.map(',', param);
 97             addSc(url, _cbKey + "=" + cb + "&" + qs);
 98         },
 99 
100         getCustom: function (url, param) {
101             addSc(url, this.map(',', param));
102         },
103 
104         map: function (split, params) {
105             var str, strs = [];
106             if (typeof params != "string") {
107                 params = eval(params);
108                 for (var p in params) {
109                     strs.push(p + "=" + params[p])
110                 }
111                 str = strs.join(split);
112             } else {
113                 str = param;
114             }
115             return str;
116         },
117         clear: function () { clearSc(); }
118     };
119 
120     _ap.fn.init.prototype = _ap.fn;
121 
122     window.CrossLoad = _ap.fn.init();
123 
124 })(window);
125 
126 
127 
128 
129 // 使用 、
130 <script>
131     window.CrossLoad.getEasy("https://www.oceanho.com/common/chat/v2","ReciveMessageHandler",{})
132 
133     function ReciveMessageHandler(msg){
134 
135     }
136 </script>

 

posted on 2015-10-21 12:08  oceanho  阅读(1420)  评论(0编辑  收藏  举报

导航