js根据手机客户端浏览器类型,判断跳转官网/手机网站多个实例代码

前端之家收集整理的这篇文章主要介绍了js根据手机客户端浏览器类型,判断跳转官网/手机网站多个实例代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

实例一、比较简单粗暴缺少点类型判断

var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"; var bIsIphoneOs = 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) == "ucweb"; var bIsAndroid = sUserAgent.match(/android/i) == "android"; var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM ){ window.location.href="3g.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>"; }

实例二、代码工整,很多的函数都基于这个增加功能

var pc_style = "" var browser = { versions: function () { var u = navigator.userAgent,app = navigator.appVersion; return { trident: u.indexOf('Trident') > -1,presto: u.indexOf('Presto') > -1,webKit: u.indexOf('AppleWebKit') > -1,gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/) && u.indexOf('QIHU') && u.indexOf('QIHU') > -1 && u.indexOf('Chrome') < 0,ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1,iPad: u.indexOf('iPad') > -1,webApp: u.indexOf('Safari') == -1,ua: u }; }(),language: (navigator.browserLanguage || navigator.language).toLowerCase() }
if (browser.versions.mobile && !browser.versions.iPad) {
this.location = "此处输入<a href="https://www.jb51.cc/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>微站<a href="https://www.jb51.cc/tag/shouye/" target="_blank" class="keywords">首页</a>地址";
}

实例三、增加了一些功能

实例四、简洁编程之家推荐使用,如果您有更好的代码可以分享

-1,ucSB : u.indexOf('Firefox/1.') > -1 }; } (),liulanqi : navigator.userAgent } if (browser.versions.QQbrw){ document.write("qq浏览器"); }else { document.write("其它浏览器"+browser.liulanqi); }
原文链接:https://www.f2er.com/js/49059.html

猜你在找的JavaScript相关文章