一、移動端打電話html
window.location.href = ("tel:" + phone);
二、移動端發送短信--Android、iOSandroid
var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // android終端或者uc瀏覽器 var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端 //sms:10086?body=1008611 sms:10086&body=1008611 if(isAndroid == true) { window.location.href=("sms:10694006929598?body="+text); } else if(isiOS == true) { window.location.href=("sms:10694006929598&body="+text); }
手機號碼ios
/^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\d{8}$/
電子郵箱c++
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/
刪除 emoji 表情git
str.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,'');
<script>json
var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, showError); } else { x.innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { x.innerHTML = "Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude; } function showError(error) { switch (error.code) { case error.PERMISSION_DENIED: x.innerHTML = "User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML = "Location information is unavailable." break; case error.TIMEOUT: x.innerHTML = "The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML = "An unknown error occurred." break; } }
</script>
錯誤代碼:
-Permission denied - 用戶不容許地理定位
-Position unavailable - 沒法獲取當前位置
-Timeout - 操做超時
-Unknown error - 未知錯誤數組
Array.prototype.unique_filterArray = Array.prototype.unique_filterArray || function(){ return this.filter(function(item, index, arr){ return arr.indexOf(item) === index; }); } Array.prototype.unique = function(){ var res = []; var json = {}; for(var i = 0; i < this.length; i++){ if(!json[this[i]]){ res.push(this[i]); json[this[i]] = 1; } } return res; }; arr.unique();//調用 Array.prototype.unique Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }; 使用JQ刪除某一項 -- arr.splice($.inArray(item,arr),1);
*將GET參數按照鍵值對的形式輸出json
var str = 'http://item.taobao.com/item.h...';*瀏覽器
function getUrl(str) { var data1=str.split("?")[1]; var result={}; if(data1.indexOf("&") > -1) { var bigArr=data1.split("&"); for(var a=0,b=bigArr.length;a<b;a++) { var smallArr=bigArr[a].split("="); result[smallArr[0]]=smallArr[1]; } } else { var arr1=data1.split("="); result[arr1[0]]=arr1[1]; } console.log(result); return result; } getUrl("http://10.8.15.176:666/wx.html?a=1");//{a:"1"} getUrl("http://10.8.15.176:666/wx.html?a=1&b=2&c=3&d");//{a: "1", b: "2", c: "3", d: undefined}
解析單個參數安全
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; } http://10.8.15.176:666/bindSuccess/coupons.html?values=uri1&gets=uri2&types=uri3 var values=getQueryString("values") ||300, gets=getQueryString("gets"), types=getQueryString("types");
一、實現arr.forEach() IE8及如下不支持原生 Array.prototype.forEach
參考底部 Array.prototype.forEach服務器
if (!Array.prototype.forEach) { Array.prototype.forEach = function(callback, thisArg) { var T, k; if (this == null) { throw new TypeError('this is null or not defined'); } var O = Object(this); var len = O.length >>> 0; // 全部非數值轉換成0;全部大於等於 0 等數取整數部分 if (typeof callback !== 'function') { throw new TypeError(callback + ' is not a function'); } if (arguments.length > 1) { T = thisArg; } k = 0; while (k < len) { var kValue; if (k in O) { kValue = O[k]; callback.call(T, kValue, k, O); } k++; } }; }
二、實現arr.filter()
參考底部 Array.prototype.filter
if (!Array.prototype.filter){ Array.prototype.filter = function(func, thisArg) { 'use strict'; if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) ) throw new TypeError(); var len = this.length >>> 0, res = new Array(len), // preallocate array t = this, c = 0, i = -1; if (thisArg === undefined){ while (++i !== len){ // checks to see if the key was set if (i in this){ if (func(t[i], i, t)){ res[c++] = t[i]; } } } } else{ while (++i !== len){ // checks to see if the key was set if (i in this){ if (func.call(thisArg, t[i], i, t)){ res[c++] = t[i]; } } } } res.length = c; // shrink down array to proper size return res; }; }
var cookieUtil = {
get: function(name) { var cookieName = encodeURIComponent(name) + "=", cookieStart = document.cookie.indexOf(cookieName), cookieValue = null; if(cookieStart > -1) { var cookieEnd = document.cookie.indexOf(';', cookieStart); if(cookieEnd === -1) { cookieEnd = document.cookie.length; } cookieValue = decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length, cookieEnd)); } return cookieValue; }, set: function(name, value, expires, path, domain, secure) { var cookieText = encodeURIComponent(name)+'='+encodeURIComponent(value); if(expires instanceof Date) { cookieText += '; expires=' + expires.toGMTString(); } else if(typeof expires === 'number') { cookieText += '; expires=' + (new Date(expires*24*60*60*1000+Date.now())).toGMTString(); } (new Date(毫秒數)).toGMTString() 7天后 (new Date(7*24*60*60*1000+Date.now())).toGMTString() if(path) { cookieText += '; path=' + path; } if(domain) { cookieText += '; domain=' + domain; } if(secure) { cookieText += '; secure'; } document.cookie = cookieText; }, unset: function(name, path, domain, secure) { this.set(name, '', new Date(0), path, domain, secure); }
}name: cookie惟一的名稱 cookie必須通過URL編碼 不區分大小寫 實踐中最好看成cookie區分大小寫value: 字符串值 必須通過URL編碼expires: 失效時間 cookie什麼時候被刪除的時間戳 默認狀況下會話結束當即將全部cookie刪除path: 域 全部向該域的請求中都會包含cookie 能夠包含子域名 也能夠不包含domain: 路徑 對於指定域中的那個路徑 應該向服務器發送cookiesecure: 安全標誌 指定後,cookie只有在使用SSL鏈接時纔會發送到服務器 是cookie中惟一一個非名值對的,直接包含secure