详解JS判断页⾯是在⼿机端还是在PC端打开的⽅法
我们想要的效果是pc⽂件和mobile⽂件统⼀⼊⼝,适配不同的设备。
先看看项⽬的⽬录:
在index.html⾥⾯配置js控制选择那⼀个⽂件夹下的⽂件就可以了。
html手机网站我们要利⽤:Navigator 对象,Navigator 对象包含有关浏览器的信息。
index.html很简单,直接上码吧:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<script type="text/javascript">
function browserRedirect() {
var sUserAgent = LowerCase();
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="f.jcngame/fanfan20171208/mobile/index.html" rel="external nofollow" rel="external nofollow" ;
} else {
//跳转pc端页⾯
window.location.href="f.jcngame/fanfan20171208//fanmai/index.html" rel="external nofollow" rel="external nofollow" ;
}
}
browserRedirect();
</script>
</head>
<body>
</body>
</html>
补充,感觉之前代码太冗余了,现在⽤正则来优化了⼀下:
<script type="text/javascript">
function browserRedirect() {
var sUserAgent = LowerCase();
if (/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(sUserAgent)) {
//跳转移动端页⾯
window.location.href="f.jcngame/fanfan20171208/mobile/index.html" rel="external nofollow" rel="external nofollow" ;
} else {
//跳转pc端页⾯
window.location.href="f.jcngame/fanfan20171208//fanmai/index.html" rel="external nofollow" rel="external nofollow" ;
}
}
browserRedirect();
</script>
以上所述是⼩编给⼤家介绍的JS判断页⾯是⼿机端还是在PC端打开的⽅法详解整合,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论