在前端開發中經常會遇到一系列兼容性問題,怎樣精准地識別出各個瀏覽器的版本是一件很頭痛的問題,尤其是navigator有可能被廠商偽造的情況。雖然大神們建議要針對瀏覽器特性來處理而不是通過瀏覽器版本,但是實際操作過程中卻有相當大的困難。
以下是大神們總結的各種瀏覽器嗅探的辦法,親測無誤
IE篇:
ie=!!window.VBArray
ie678=!+"\v1"; ie678=!-[1,]; ie678='\v'=='v'; ie678=('a-b'.split(/(~)/))[1]=="b" ie678=0.9.toFixed(0)=="0" ie678=/\w/.test('\u0130') ie8=window.toStaticHTML ie9=window.msPerformance ie678=0//@cc_on+1; ie67=!"1"[0] ie8=!!window.XDomainRequest; ie9=document.documentMode&&document.documentMode===9; ie10 = window.navigator.msPointerEnabled; ie11 = !!window.MSInputMethodContext;
Firefox篇:
firefox=!!window.netscape;
firefox=!!window.updateCommands;
safari=window.openDatabase&&!window.chrome;
chrome=!!(window.chrome&&window.google);
移動設備篇:
iphone=/iphone/i.test(navigator.userAgent);
iphone4=window.devicePixelRatio>=2;
ipad=/iPad/i.test(navigator.userAgent);
android=/android/i.test(navigator.userAgent);
IOS=iphone||ipad;