navigator对象包含有关浏览器的信息,它有很多属性,我们最常用的是userAgent ,该属性可以返回由客户机发送服务器的user- agent头部的值。
下面前端代码可以判断用户那个终端打开页面,实现跳转
[JavaScript] 纯文本查看 复制代码 if ( (navigator . userAgent . match (/ (phone I padIpodI iPhone liPodI iosI iPad |Android I
Mobile IBlackBerryl IEMobi le IMQQBrowser | JUC I Fennec IwOSBrowser I Browse rNG I Webos
I Symbian |Windows Phone)/i) )){
window. location.href =”";
//手机
} else {
window. location.href = "";
//电脑
}
如果上面方法没效果,可以使用下面的方法:
[JavaScript] 纯文本查看 复制代码 <script type="text/javascript">
function browserRedirect() {
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 = "../bbs/forum.php?mod=guide&view=hot&mobile=2";
}
}
browserRedirect();
</script>
|