通過判斷打開設(shè)備闻妓,跳轉(zhuǎn)不同頁面然爆,可以根據(jù) User-Agent 來區(qū)分:
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows?Phone)/i)))?{????window.location.href?=?"";?????//手機(jī)}?else?{????window.location.href?=?"";????????//電腦}
也可以執(zhí)行其他操作:
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows?Phone)/i)))?{????alert('您正在通過手機(jī)訪問');}?else?{????alert("您在PC端訪問");}
JS判斷客戶端是否是iOS或者Android手機(jī)移動(dòng)端:
通過判斷瀏覽器的 userAgent次和,用正則來判斷手機(jī)是否是ios和Android客戶端一姿。代碼如下:
<script?type="text/javascript">????var?u?=?navigator.userAgent;????var?isAndroid?=?u.indexOf('Android')?>?-1?||?u.indexOf('Adr')?>?-1;?//android終端????var?isiOS?=?!!u.match(/\(i[^;]+;(?U;)??CPU.+Mac?OS?X/);?//ios終端????alert('是否是Android:'+isAndroid);????alert('是否是iOS:'+isiOS);</script>
下面一個(gè)比較全面的瀏覽器檢查函數(shù)包各,提供更多的檢查內(nèi)容摘仅,你可以檢查是否是移動(dòng)端(Mobile)、ipad髓棋、iphone实檀、微信、QQ等按声。
第一種:
<script?type="text/javascript">????//判斷訪問終端????var?browser={????????versions:function(){????????????var?u?=?navigator.userAgent,?app?=?navigator.appVersion;????????????return?{????????????????trident:?u.indexOf('Trident')?>?-1,?//IE內(nèi)核????????????????presto:?u.indexOf('Presto')?>?-1,?//opera內(nèi)核????????????????webKit:?u.indexOf('AppleWebKit')?>?-1,?//蘋果膳犹、谷歌內(nèi)核????????????????gecko:?u.indexOf('Gecko')?>?-1?&&?u.indexOf('KHTML')?==?-1,//火狐內(nèi)核????????????????mobile:?!!u.match(/AppleWebKit.*Mobile.*/),?//是否為移動(dòng)終端????????????????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應(yīng)該程序,沒有頭部與底部????????????????weixin:?u.indexOf('MicroMessenger')?>?-1,?//是否微信?(2015-01-22新增)????????????????qq:?u.match(/\sQQ/i)?==?"?qq"?//是否QQ????????????};????????}(),????????language:(navigator.browserLanguage?||?navigator.language).toLowerCase()????}????//使用方法:????//判斷是否IE內(nèi)核????if(browser.versions.trident){?alert("is?IE");?}????//判斷是否webKit內(nèi)核????if(browser.versions.webKit){?alert("is?webKit");?}????//判斷是否移動(dòng)端????if(browser.versions.mobile||browser.versions.android||browser.versions.ios){????????alert("移動(dòng)端");?
????}</script>
檢測(cè)瀏覽器語言
currentLang?=?navigator.language;???//判斷除IE外其他瀏覽器使用語言if(!currentLang){//判斷IE瀏覽器使用語言????currentLang?=?navigator.browserLanguage;}alert(currentLang);
第二種:
if?(/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent))?{????//alert(navigator.userAgent);??????window.location.href?="iPhone.html";}?else?if?(/(Android)/i.test(navigator.userAgent))?{????//alert(navigator.userAgent);?????window.location.href?="Android.html";}?else?{????window.location.href?="pc.html";};
也可以通過這樣來適配签则,然后直接轉(zhuǎn)跳到移動(dòng)端頁面:
function?mobile_device_detect(url){??var?thisOS=navigator.platform;??var?os=new?Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows?Phone","Phone","Linux?armv71","MAUI","UNTRUSTED/1.0","Windows?CE","BlackBerry","IEMobile");??for(var?i=0;i<os.length;i++){????if(thisOS.match(os[i])){??????window.location.href=url;????}??}??if(navigator.platform.indexOf('iPad')?!=?-1){????window.location.href=url;??}??var?check?=?navigator.appVersion;??if(?check.match(/linux/i)?){????if(check.match(/mobile/i)?||?check.match(/X11/i)){??????window.location.href=url;????}??}}