Created
February 25, 2014 05:28
-
-
Save zhuzhuaicoding/9203256 to your computer and use it in GitHub Desktop.
$loadScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function $loadScript(obj) { | |
| if (!$loadScript.counter) { | |
| $loadScript.counter = 1; | |
| } | |
| var isObj = typeof(obj) == "object", url = isObj ? obj.url : arguments[0], id = isObj ? obj.id : arguments[1], obj = isObj ? obj : arguments[2], _head = document.head || document.getElementsByTagName("head")[0] || document.documentElement, _script = document.createElement("script"), D = new Date(), _time = D.getTime(), _isCleared = false, _timer = null, o = obj || {}, data = o.data || '', charset = o.charset || "gb2312", isToken = o.isToken, skey = o.skey, timeout = o.timeout, isAutoReport = o.isAutoReport || false, reportOptions = o.reportOptions || {}, reportType = o.reportType || 'current', reportRetCodeName = o.reportRetCodeName, reportSuccessCode = typeof(o.reportSuccessCode) == "undefined" ? 200 : o.reportSuccessCode, reportErrorCode = typeof(o.reportErrorCode) == "undefined" ? 500 : o.reportErrorCode, reportTimeoutCode = typeof(o.reportTimeoutCode) == "undefined" ? 600 : o.reportTimeoutCode, onload = o.onload, onsucc = o.onsucc, callbackName = o.callbackName || '', callback = o.callback, errorback = o.errorback, _jsonpLoadState = 'uninitialized'; | |
| var complete = function (errCode) { | |
| if (!_script || _isCleared) { | |
| return; | |
| } | |
| _isCleared = true; | |
| if (_timer) { | |
| clearTimeout(_timer); | |
| _timer = null; | |
| } | |
| _script.onload = _script.onreadystatechange = _script.onerror = null; | |
| if (_head && _script.parentNode) { | |
| _head.removeChild(_script); | |
| } | |
| _script = null; | |
| if (callbackName) { | |
| if (callbackName.indexOf('.') == -1) { | |
| window[callbackName] = null; | |
| try { | |
| delete window[callbackName]; | |
| } | |
| catch (e) { | |
| } | |
| } | |
| else { | |
| var arrJ = callbackName.split("."), p = {}; | |
| for (var j = 0, jLen = arrJ.length; j < jLen; j++) { | |
| var n = arrJ[j]; | |
| if (j == 0) { | |
| p = window[n]; | |
| } | |
| else { | |
| if (j == jLen - 1) { | |
| try { | |
| delete p[n]; | |
| } | |
| catch (e) { | |
| } | |
| } | |
| else { | |
| p = p[n]; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| if (_jsonpLoadState != "loaded" && typeof errorback == "function") { | |
| errorback(errCode); | |
| } | |
| if (isAutoReport && reportType != 'cross') { | |
| _retCoder.report(_jsonpLoadState == "loaded", errCode); | |
| } | |
| }; | |
| var jsontostr = function (d) { | |
| var a = []; | |
| for (var k in d) { | |
| a.push(k + '=' + d[k]); | |
| } | |
| return a.join('&'); | |
| }; | |
| if (isAutoReport && reportOptions) { | |
| if (reportType == 'cross') { | |
| $returnCode(reportOptions).reg(); | |
| } | |
| else { | |
| reportOptions.url = reportOptions.url || url.substr(0, url.indexOf('?') == -1 ? url.length : url.indexOf('?')); | |
| var _retCoder = $returnCode(reportOptions); | |
| } | |
| } | |
| if (data) { | |
| url += (url.indexOf("?") != -1 ? "&" : "?") + (typeof data == 'string' ? data : jsontostr(data)); | |
| } | |
| if (callbackName && typeof callback == "function") { | |
| var oldName = callbackName; | |
| if (callbackName.indexOf('.') == -1) { | |
| callbackName = window[callbackName] ? callbackName + $loadScript.counter++ : callbackName; | |
| window[callbackName] = function (jsonData) { | |
| _jsonpLoadState = 'loaded'; | |
| if (isAutoReport && reportRetCodeName) { | |
| reportSuccessCode = jsonData[reportRetCodeName]; | |
| } | |
| callback.apply(null, arguments); | |
| onsucc && (onsucc()); | |
| }; | |
| } | |
| else { | |
| var arrJ = callbackName.split("."), p = {}, arrF = []; | |
| for (var j = 0, jLen = arrJ.length; j < jLen; j++) { | |
| var n = arrJ[j]; | |
| if (j == 0) { | |
| p = window[n]; | |
| } | |
| else { | |
| if (j == jLen - 1) { | |
| p[n] ? (n = n + $loadScript.counter++) : ''; | |
| p[n] = function (jsonData) { | |
| _jsonpLoadState = 'loaded'; | |
| if (isAutoReport && reportRetCodeName) { | |
| reportSuccessCode = jsonData[reportRetCodeName]; | |
| } | |
| callback.apply(null, arguments); | |
| onsucc && (onsucc()); | |
| }; | |
| } | |
| else { | |
| p = p[n]; | |
| } | |
| } | |
| arrF.push(n); | |
| } | |
| callbackName = arrF.join('.'); | |
| } | |
| url = url.replace('=' + oldName, '=' + callbackName); | |
| } | |
| _jsonpLoadState = 'loading'; | |
| id = id ? (id + _time) : _time; | |
| url = (isToken !== false ? $addToken(url, "ls", skey) : url); | |
| _script.charset = charset; | |
| _script.id = id; | |
| _script.onload = _script.onreadystatechange = function () { | |
| var uA = navigator.userAgent.toLowerCase(); | |
| if (!(!(uA.indexOf("opera") != -1) && uA.indexOf("msie") != -1) || /loaded|complete/i.test(this.readyState)) { | |
| if (typeof onload == "function") { | |
| onload(); | |
| } | |
| complete(_jsonpLoadState == "loaded" ? reportSuccessCode : reportErrorCode); | |
| } | |
| }; | |
| _script.onerror = function () { | |
| complete(reportErrorCode); | |
| }; | |
| if (timeout) { | |
| _timer = setTimeout(function () { | |
| complete(reportTimeoutCode); | |
| }, parseInt(timeout, 10)); | |
| } | |
| setTimeout(function () { | |
| _script.src = url; | |
| try { | |
| _head.insertBefore(_script, _head.lastChild); | |
| } catch (e) { | |
| } | |
| }, 0); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment