利用JS判断客户端类型你应该知道的四种方法(2)
2.2 使用方法
/判断是否IE内核 if(browser.versions.trident){ alert("is IE"); } //判断是否webKit内核 if(browser.versions.webKit){ alert("is webKit"); } //判断是否移动端 if(browser.versions.mobile||browser.versions.android||browser.versions.ios){ alert("移动端"); }
2.3 检测浏览器语言
currentLang = navigator.language; //判断除IE外其他浏览器使用语言 if(!currentLang){//判断IE浏览器使用语言 currentLang = navigator.browserLanguage; } alert(currentLang);
3. 判断iPhone|iPad|iPod|iOS|Android客户端
代码如下:
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS //alert(navigator.userAgent); window.location.href ="iPhone.html"; } else if (/(Android)/i.test(navigator.userAgent)) { //判断Android //alert(navigator.userAgent); window.location.href ="Android.html"; } else { //pc window.location.href ="pc.html"; };
4. 判断pc还是移动端
代码如下:
<script> //判断是否手机端访问 var userAgentInfo = navigator.userAgent.toLowerCase(); var Agents = ["android", "iphone", "symbianos", "windows phone", "ipad", "ipod"]; var ly=document.referrer; //返回导航到当前网页的超链接所在网页的URL for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) >= 0&&(ly==""||ly==null)) { this.location.href='http://m.***.com'; //wap端地址 } } </script>
5. 常用跳转代码
看代码
<script type="text/javascript"> // borwserRedirect (function browserRedirect(){ var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAgent.match(/ipad/i) == 'ipad'; var bIsIphone = sUserAgent.match(/iphone os/i) == 'iphone os'; var bIsMidp = sUserAgent.match(/midp/i) == 'midp'; var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == 'rv:1.2.3.4'; var bIsUc = sUserAgent.match(/ucweb/i) == 'web'; var bIsCE = sUserAgent.match(/windows ce/i) == 'windows ce'; var bIsWM = sUserAgent.match(/windows mobile/i) == 'windows mobile'; var bIsAndroid = sUserAgent.match(/android/i) == 'android'; var pathname = location.pathname if(bIsIpad || bIsIphone || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM || bIsAndroid ){ window.location.href = 'http://m.geekjc.com'+pathname; //wap端地址 } })(); </script>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对黑区网络的支持。