// 獲取地址欄參數 function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null; } // 判斷瀏覽器內核,手機類型 function checkVersion(){ var u = navigator.userAgent, app = navigator.appVersion; return { trident: u.indexOf('Trident') > -1, //IE內核 presto: u.indexOf('Presto') > -1, //opera內核 webKit: u.indexOf('AppleWebKit') > -1, //蘋果、谷歌內核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐內核 mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否爲移動終端 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios終端 android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android終端 iPhone: u.indexOf('iPhone') > -1 , //是否爲iPhone或者QQHD瀏覽器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1, //是否web應該程序,沒有頭部與底部 weixin: u.indexOf('MicroMessenger') > -1, //是否微信 qq: u.match(/\sQQ/i) == " qq", //是否QQ app: u.indexOf('erbanApp') > -1 //是否在app內 }; } // 圖片預加載 function preloadImage(obj){ var loadLength = 0,newImages = []; for(var i = 0;i < obj.imageArr.length;i++){ newImages[i] = new Image(); newImages[i].src = obj.imageArr[i]; newImages[i].onload = newImages[i].onerror = function(){ loadLength++; typeof obj.preloadPreFunc === 'function' && obj.preloadPreFunc(loadLength); if(loadLength == obj.imageArr.length){ typeof obj.doneFunc === 'function' && obj.doneFunc(); } } } } // 判斷是否在App內 function isApp() { var androidBol = false; var osBol = false; if(window.androidJsObj && typeof window.androidJsObj === 'object'){ androidBol = true; } if(window.webkit){ osBol = true; } return (androidBol || osBol); } //得到某個元素距離目標元素的左邊和上面的距離 function offset(dom,parentDom) { var offsetL = 0, offsetT = 0, parentDom=parentDom||window.document.body, obj = {}; while (dom != parentDom && dom != null) { offsetL += dom.offsetLeft offsetT += dom.offsetTop dom = dom.offsetParent } obj.top = offsetT; obj.left = offsetL; return obj } //得到哈希值 function getHash() { var url = location.hash; //獲取url中"#"符後的字串 var theHash = {}; var str = url.substr(1); if(str.length<1)return {}; var strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theHash[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } return theHash; } //刪除哈希值 function delHash(k) { var hash = getHash()||{}; var str = ''; if(hash[k]){ delete hash[k]; } for (var key in hash) { str += key + '=' + hash[key] + '&'; } location.hash = str.substr(0, str.length - 1); } //添加哈希值 function addHash(key, value) { var hash = getHash()||{}; var str = ''; hash[key] = value; for (var key in hash) { str += key + '=' + hash[key] + '&'; } location.hash = str.substr(0, str.length - 1); } //動態加載js function loadScript(src, callback) { var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState) { //IE script.onreadystatechange = function() { if (script.readyState == "loaded" || script.readyState == "complete") { script.onreadystatechange = null; callback(); } }; } else { //Others: Firefox, Safari, Chrome, and Opera script.onload = function() { callback(); }; } script.src = src; document.body.appendChild(script); } //拷貝數據,僅限數組和對象 function copy(obj) { return JSON.parse(JSON.stringify(obj)); } //瀏覽器隱藏顯示tab的回調 function handleVisibilityChange(visibleCallback, hiddenCallback) { var hidden, visibilityChange; if (typeof document.hidden !== "undefined") { hidden = "hidden"; visibilityChange = "visibilitychange"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; visibilityChange = "msvisibilitychange"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; visibilityChange = "webkitvisibilitychange"; } if (typeof document[hidden] === "undefined") { console.log("This browser not support visibilityChange"); } else { //document.addEventListener(visibilityChange, handleVisibilityChange, false); addEvent(document, visibilityChange, function() { if (document[hidden]) { hiddenCallback(); } else { visibleCallback(); } }) } } //添加事件監聽兼容性寫法 function addEvent() { if (document.addEventListener) { return function(el, type, fn) { el.addEventListener(type, fn, false); }; } else { return function(el, type, fn) { el.attachEvent('on' + type, function() { return fn.call(el, window.event); }); }; } }; function setCookie(name,value){ document.cookie = name + "="+ escape (value); } function getCookie(name) { var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return unescape(arr[2]); else return null; } function base64_encode(input) { var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = utf8_encode(input); while (i < input.length) { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4); } return output; }; function utf8_encode(string) { string = string.replace(/\r\n/g,"\n"); let utftext = ""; for (let n = 0; n < string.length; n++) { let c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; } function base64_decode (input) { // 解碼,配合decodeURIComponent使用 var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); while (i < input.length) { enc1 = base64EncodeChars.indexOf(input.charAt(i++)); enc2 = base64EncodeChars.indexOf(input.charAt(i++)); enc3 = base64EncodeChars.indexOf(input.charAt(i++)); enc4 = base64EncodeChars.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2); } if (enc4 != 64) { output = output + String.fromCharCode(chr3); } } return utf8_decode(output); } function isObj(obj){ return Object.prototype.toString.call(obj) == '[object Object]'; } function isFun(fun){ return Object.prototype.toString.call(fun) == '[object Function]'; } function isArr(arr){ return Object.prototype.toString.call(arr) == '[object Array]'; } function isNum(arr){ return Object.prototype.toString.call(arr) == '[object Number]'; } //是不是json字符串 function isJSONStr(str) { if (typeof str == 'string') { try { var obj = JSON.parse(str); if (typeof obj == 'object') { return true; } else { return false; } } catch (e) { return false; } } else { return false; } } export { offset, isApp, preloadImage, checkVersion, getQueryString, getHash, delHash, addHash, loadScript, copy, handleVisibilityChange, addEvent, base64_encode, utf8_encode, base64_decode, isObj, isFun, isArr, isNum, isJSONStr }