利用正则表达式判断手机端还是PC端
<script type="text/javascript">
function isPC() {
var userAgent = navigator.userAgent.toLowerCase();
if (/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(userAgent)) {
//跳转移动端页面
window.location.href="http://a.name.com/mobile/index.html";
} else {
//跳转pc端页面
window.location.href="http://b.name.com/pc/index.html";
}
}
</script>
获取移动端设备
<script type="text/javascript">
function getOs() {
var os = ''
var userAgent = window.navigator.userAgent.toLowerCase();
if (!!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
// ios端
os = 'ios'
}
else if (userAgent.indexOf('android') > -1 || userAgent.indexOf('Adr') > -1) {
// android端
os = 'android'
}
if (userAgent.match(/WeiBo/i) == "weibo") { // 微博
os = 'wb'
}
if (userAgent.match(/MicroMessenger/i) == 'micromessenger') {
// 微信浏览器
os = 'wx'
}
if (userAgent.match(/QQ/i) == "qq") {
//在QQ空间打开
os = 'qq'
}
alert(os)
return os
}
</script>
本文介绍了一种使用正则表达式来判断用户所使用的设备类型(如iOS、Android等)的方法,并提供了相应的JavaScript代码实现。此外,还展示了如何根据不同设备类型进行页面跳转。





