/* funname preventEventPropagation * desc 阻止冒泡事件&阻止默認行爲&阻止事件捕獲 * params {name: string}參數名稱 * return {string or null} */ var preventEventPropagation = function(evt) { var e = evt || window.event; if (e) { e.preventDefault(); e.stopPropagation(); } return false; };
/* funname getQueryString * desc 獲取地址參數 * params {name: string}參數名稱 * return {string or null} */ var getQueryString = function(name) { var r, reg; reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); r = window.location.search.substr(1).match(reg); if (r !== null) { return r[2]; //不解碼 } return null; };
/* *desc:判斷微信環境 *params:null */ var isInWechat = function() { var ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i) == "micromessenger") { return true; } else { return false; } };
/* *funname:platformFn *des:獲取環境參數 *params: *return: [object] */ var platformFn = function() { var u = navigator.userAgent, app = navigator.appVersion; return { webKit: u.indexOf('AppleWebKit') > -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, //是否微信 (2015-01-22新增) }; };
// 判斷是否爲數組 /* *funname:isArray *des:判斷是否爲數組 *params: [array] arr *return: [bolean] */ function isArray(arr) { if (arr typeof arr === 'object' && arr.constructor == Array) { return true; } return false; }
// 數組除重 /* *funname:arrayUnique *des:數組去重 *params: [array] arr *return: [array] */ var arrayUnique = function(arr) { var newArr = []; var temp = {}; var len = arr.length; for (var index = 0; index < len; index++) { var key = typeof(arr[index]) + arr[index]; if (temp[key] !== 1) { newArr.push(arr[index]); temp[key] = 1; } } return newArr; };
/* *funname:getCookie *des:獲取cookie *params: [array] arr *return: [array] */ function getCookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(c_name + "=") if (c_start != -1) { c_start = c_start + c_name.length + 1 c_end = document.cookie.indexOf(";", c_start) if (c_end == -1) c_end = document.cookie.length return unescape(document.cookie.substring(c_start, c_end)) } } return "" }
/* *funname:setCookie *des:設置cookie *params: [string] c_name,[string] value,expiredays *return: [array] */ function setCookie(c_name, value, expiredays) { var exdate = new Date() exdate.setDate(exdate.getDate() + expiredays) document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) }
/* *funname:checkCookie *des:檢測cookie *params: *return: */ function checkCookie() { username = getCookie('username') if (username != null && username != "") { return true; } else { return false; } }
/* *funname:getLocalStorage *des:獲取localstorage *params: [string] key *return: [string] */ function getLocalStorage(key) { if (localStorage) { return localStorage.getItem(key); } else { // 使用cookie獲取 return getCookie(key); } return ''; }
/* *funname:setLocalStorage *des:設置localstorage *params: *return: null */ function setLocalStorage(key, value, expiredays) { if (localStorage) { console.log(setLocalStorage); localStorage.setItem(key, value); } else { // 使用cookie保存 setCookie(key, value, expiredays); } }
/* *funname:clearLocalStorage *des:清空localstorage *params: [string] key *return: [string] */ function clearLocalStorage(key) { if (localStorage) { localStorage.removeItem(key); } else { // 使用cookie獲取 clearCookie(key) } }
/** * ScrollFix v0.1 * http://www.joelambert.co.uk * * Copyright 2011, Joe Lambert. * Free to use under the MIT license. * http://www.opensource.org/licenses/mit-license.php * des: 解決ios滾動問題的插件 */ var ScrollFix = function(elem) { // Variables to track inputs var startY, startTopScroll; elem = elem || document.querySelector(elem); // If there is no element, then do nothing if (!elem) return; // Handle the start of interactions elem.addEventListener('touchstart', function(event) { startY = event.touches[0].pageY; startTopScroll = elem.scrollTop; if (startTopScroll <= 0) elem.scrollTop = 1; if (startTopScroll + elem.offsetHeight >= elem.scrollHeight) elem.scrollTop = elem.scrollHeight - elem.offsetHeight - 1; }, false); };
/* *funname:loadScript *des:動態加載js *params: [string] filepath,[function] onloadCallback *return: null */ function loadScript(filepath, onloadCallback) { var scriptDom = document.createElement("script"); scriptDom.onload = scriptDom.onreadystatechange = onloadCallback; scriptDom.type = "text/javascript"; scriptDom.src = filepath; document.body.appendChild(scriptDom); }
/* *desc:針對純 JSON 數據對象的深拷貝 *params:[object] obj */ var cloneObj = function(obj) { if (obj) { var newObj = JSON.stringify(obj) newObj = JSON.parse(newObj) return newObj } return null }; 侷限性: - 沒法複製函數 - 原型鏈沒了,對象就是object,所屬的類沒了。
/* *desc:移動端返回上一頁 *params: */ var appGoBack = function() { if (!url || url == '') { window.history.go(-1); setTimeout(function() { // 若是不支持history.go,返回首頁 location.replace('/'); }, 300); } else { // ios的.history.go和history.back有bug location.replace(url); } return false; }
/* * funname:GetRandomNum *params:[number] * des:獲取隨機數 *return: */ var getRandom = function(Min, Max) { var Range = Max - Min; var Rand = Math.random(); return (Min + Math.round(Rand * Range)); };
function checkPhone(phone ){ if(/^1(3|4|5|7|8)\d{9}$/.test(phone)){ return true; } return false; }
function isChinese(str){ var reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/; return reg.test(str); } //u4e00-u9fbf: unicode CJK(中日韓)統一表意字符。u9fa5後至u9fbf爲空 //uF900-uFAFF: unicode CJK 兼容象形文字 。uFA2D後至uFAFF爲空
不按期更新,看心情更新~javascript
參考連接:http://www.cnblogs.com/baiyyg...php