用JS判断用户使用的是手机端还是pc端访问
2013-10-12 10:53
645 查看
最近项目中用到一个应用,当访问同一个网站地址的时候,例如:www.xxx.com的时候,如果当前客户端是pc则跳转到专注于pc的部分,如果当前客户机是手机,则跳转到专注于手机的部分,秉承一贯的习惯,baidu or google,但发觉网上的解决办法都不尽如人意,很多都是通过js读取本地文件系统进行判断,但经过测试,不能成功,而且通过js读取本地文件系统会造成安全性问题,但作为开放的互联网,我们不可能为每一部电脑设置安全性,于是自己动手,丰衣足食,以下就是我的解决办法:
依然是用js,不过只需要用到 navigator.platform,这是鉴于读取这个属性并不会造成安全性问题,而且,普遍的操作系统都屈指可数
~~~navigator.platform判断系统类型,从而判断是手机还是pc
简单的跳转代码如下:
if(navigator.platform.indexOf('Win32')!=-1){
//go to pc
}else{
// go to 手机
}
-------------------------------------------------------
另一种方法 ~~~用 navigator.userAgent 判断是否现在的手机浏览器,似乎 navigator.platform 比较简单靠谱一点
用户是手机访问还是电脑方法 ~~~还是用 navigator.userAgent去判断是否为手机浏览器,不过这个比较清晰一点
代码如下 复制代码
var is_iPd = navigator.userAgent.match(/(iPad|iPod|iPhone)/i) != null;
var is_mobi = navigator.userAgent.toLowerCase().match(/(ipod|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|win ce)/i) != null;
if(is_mobi && window.location.search.indexOf("mv=fp")<0){
window.location.href="#";
}
浏览器类型
代码如下 复制代码
if(navigator.userAgent.indexOf("MSIE")>0){
//ie
}else if(navigator.userAgent.indexOf("Firefox")>0){
//firefox
}else if(navigator.userAgent.indexOf("Chrome")>0){
//chrome
}else if(navigator.userAgent.indexOf("Safari")>0){
//safari
}else{
//this part can be used as opera area
}
<script type="text/javascript">
<!--
//平台、设备和操作系统
var system ={
win : false,
mac : false,
xll : false
};
//检测平台
var p = navigator.platform;
alert(p);
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
//跳转语句
if(system.win||system.mac||system.xll){//转向后台登陆页面
window.location.href="login.jsp";
}else{
window.location.href="wapLogin.jsp";
}
-->
</script>
依然是用js,不过只需要用到 navigator.platform,这是鉴于读取这个属性并不会造成安全性问题,而且,普遍的操作系统都屈指可数
~~~navigator.platform判断系统类型,从而判断是手机还是pc
简单的跳转代码如下:
if(navigator.platform.indexOf('Win32')!=-1){
//go to pc
}else{
// go to 手机
}
-------------------------------------------------------
另一种方法 ~~~用 navigator.userAgent 判断是否现在的手机浏览器,似乎 navigator.platform 比较简单靠谱一点
<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=B页面; } } browserRedirect(); </script> -------------------------------------------------------
用户是手机访问还是电脑方法 ~~~还是用 navigator.userAgent去判断是否为手机浏览器,不过这个比较清晰一点
代码如下 复制代码
var is_iPd = navigator.userAgent.match(/(iPad|iPod|iPhone)/i) != null;
var is_mobi = navigator.userAgent.toLowerCase().match(/(ipod|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|win ce)/i) != null;
if(is_mobi && window.location.search.indexOf("mv=fp")<0){
window.location.href="#";
}
浏览器类型
代码如下 复制代码
if(navigator.userAgent.indexOf("MSIE")>0){
//ie
}else if(navigator.userAgent.indexOf("Firefox")>0){
//firefox
}else if(navigator.userAgent.indexOf("Chrome")>0){
//chrome
}else if(navigator.userAgent.indexOf("Safari")>0){
//safari
}else{
//this part can be used as opera area
}
<script type="text/javascript">
<!--
//平台、设备和操作系统
var system ={
win : false,
mac : false,
xll : false
};
//检测平台
var p = navigator.platform;
alert(p);
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
//跳转语句
if(system.win||system.mac||system.xll){//转向后台登陆页面
window.location.href="login.jsp";
}else{
window.location.href="wapLogin.jsp";
}
-->
</script>
相关文章推荐
- 用JS判断用户使用的是手机端还是pc端访问
- 使用js判断用户访问网站是通过PC端还是移动端
- js如何判断是手机端还是PC端访问
- js判断是手机端还是PC端访问
- js判断web网站是手机端访问的还是PC端访问的
- python如何判断web访问来源是PC端还是手机端
- JS判断用户是PC端还是移动端进入
- java实现判断是手机端还是pc端访问
- 使用js判断手机端和pc端浏览器内核
- js如何判断用户是在PC端和还是移动端访问
- js判断用户是移动设备访问还是PC访问
- 判断来访问网站的用户是手机用户还是Web浏览器的用户【即判定用户当前使用的系统】
- JS判断用户使用的是手机还是电脑?
- 使用js判断是手机访问还是电脑访问
- JS如何判断用户使用安卓还是苹果
- PHP判断用户使用手机端登录还是pc端登录(企业版)
- JavaScript判断用户是通过电脑端还是移动端访问;js浏览器判断navigator
- JavaScript判断用户是通过电脑端还是移动端访问;js浏览器判断navigator
- PHP判断是手机端访问还是PC端访问网站
- PHP判断用户是手机端?还是浏览器端访问?