一,獲取客戶端狀態javascript
一、獲取cookiejava
function cookieInfo() { setcookie('cookie_test','1'); var cookie_test = getcookie('cookie_test'); if( '1' != cookie_test) { return '關閉'; }else { return '開啓'; } } function setcookie(name,value)//兩個參數,一個是cookie的名子,一個是值 { var exp = new Date(); exp.setTime(exp.getTime() + 10*1000); document.cookie = name + "="+ escape(value) + ";expires=" + exp.toGMTString(); } function getcookie(name)//取cookies函數 { var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null; }
二、獲取操做系統es6
function osInfo() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform === "Win32") || (navigator.platform === "Windows"); var isMac = (navigator.platform === "Mac68K") || (navigator.platform === "MacPPC") || (navigator.platform === "Macintosh") || (navigator.platform === "MacIntel"); var bIsIpad = sUserAgent.match("iPad"); var bIsIphoneOs = sUserAgent.match("iPhone"); var isUnix = (navigator.platform === "X11") && !isWin && !isMac; var isLinux = (String(navigator.platform).indexOf("Linux") > -1); var bIsAndroid = sUserAgent.match("Android"); var bIsCE = sUserAgent.match("Windows CE") || sUserAgent.match("WinCE") || sUserAgent.match("WindowsCE"); var bIsWM = sUserAgent.match("Windows Mobile"); var bIsWP = sUserAgent.match("Windows Phone OS"); if (bIsIpad || bIsIphoneOs) return 'IOS'; if (isMac) return "Mac"; if (isUnix) return "Unix"; if (isLinux) { if (bIsAndroid) return "Android"; else return "Linux"; } if (bIsCE) return 'Windows CE'; if (bIsWM) return 'Windows Mobile'; if (bIsWP) return 'Windows Phone'; if (sUserAgent.match("BlackBerry")) return "BlackBerry OS"; if (sUserAgent.match("RIM Tablet OS")) return "BlackBerry Tablet OS"; if (sUserAgent.match("(?:web|hpw)OS")) return "webOS"; if (sUserAgent.match("SymbianOS/9.1") || sUserAgent.match("Series[ ]?60") || sUserAgent.match("S60")) return "Series60"; if (isWin) { var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1; if (isWin2K) return "Win2000"; var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1; if (isWinXP) return "WinXP"; var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1; if (isWin2003) return "Win2003"; var isWinVista = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1; if (isWinVista) return "WinVista"; var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1; if (isWin7) return "Win7"; var isWin8 = sUserAgent.indexOf("Windows NT 6.2") > -1 || sUserAgent.indexOf("Windows 8") > -1; if (isWin8) return "Win8"; } return "other"; }
三、獲取flash狀態web
function getFlashVersion() { var flashVer = NaN; var ua = navigator.userAgent; if (window.ActiveXObject) { var swf = new ActiveXObject('ShockwaveFlash.ShockwaveFlash'); if (swf) { flashVer = Number(swf.GetVariable('$version').split(' ')[1].replace(/,/g, '.').replace(/^(d+.d+).*$/, "$1")); } } else { if (navigator.plugins && navigator.plugins.length > 0) { var swf = navigator.plugins['Shockwave Flash']; if (swf) { var arr = swf.description.split(' '); for (var i = 0, len = arr.length; i < len; i++) { var ver = Number(arr[i]); if (!isNaN(ver)) { flashVer = arr[i]; break; } } } } } if (!isNaN(flashVer)) { return flashVer; } else { return '未安裝'; } }
四、獲取瀏覽器版本chrome
function getBrowser(){ //獲取用戶瀏覽器 var W = window,B = {}; var agent = navigator.userAgent; if (W.ActiveXObject) { B.name = 'ie'; B.ver = 9 - (agent.indexOf("Trident/5.0") == -1) - (!W.XDomainRequest) - (!W.XMLHttpRequest); } else { if (document.getBoxObjectFor || W.mozInnerScreenX != undefined) { B.name = 'firefox'; B.ver= (m = /(?:Firefox|GranParadiso|Iceweasel|Minefield).(\d+\.\d+)/i.exec(agent)) ? parseFloat(m[1], 10) : 3.3 } else { if (!navigator.taintEnabled) { m = /AppleWebKit.(\d+\.\d+)/i.exec(agent); B.name = 'webkit'; B.ver = m ? parseFloat(m[1], 10) : document.evaluate ? (document.querySelector ? 525 : 420) : 419; if ((m = /Chrome.(\d+\.\d+)/i.exec(agent)) || W.chrome) { B.name = 'chrome'; B.ver = m ? parseFloat(m[1], 10) : 2 } else { if ((m = /Version.(\d+\.\d+)/i.exec(agent)) || W.safariHandler) { B.name = 'safari'; B.ver = m ? parseFloat(m[1], 10) : 3.3 } } } else { if (W.opera) { B.name = 'opera'; B.ver = parseFloat(navigator.appVersion, 10) } else { B.name = 'ie'; B.ver = 6 } } } } return B.name + ':'+B.ver; }
五、本地存儲瀏覽器
function localStorageInfo() { try { if ('localStorage' in window && window['localStorage'] !== null) { return "開啓"; } return "關閉"; } catch (e) { return "關閉"; } }
二,js 用法心得cookie
1,setTimeoutapp
setTimeout(function () { },1000);
定時器,1秒鐘後執行,這個函數是不阻塞的,代碼該往下執行就執行,不會到他這裏停滯1秒,因此這個方法不適合作和sleep相似的功能。
2,new Imageide
var img = new Image(); img.src = new_src; img.id = num; img.className = "hide";
下面這個方法是用 new 的方法增長一個監聽器,就好比說 onerror ,onclick,onload,之類的方法都須要這麼作
img.addEventListener("error", function (event) { console.log($(ths)); var x = $(event.target).attr('id'); // var x = $(img).attr('id'); console.log(x,"xxxxxxxxxxxxx") }); event 是當前這個標籤獲得對象,若是想獲取當前這個對象的內容使用event就能夠了。