用JS判断用户使用的是手机端还是pc端访问
最近项目中用到一个应用,当访问同一个网站地址的时候,例如: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 比较简单靠谱一点
<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>
最新文章
- Ionic2 Tutorial
- 13.java.lang.NoSuchFiledException
- 浅谈spring——注解配置(九)
- 基于图形检测API(shape detection API)的人脸检测
- debian 9 双显卡安装NVIDIA显卡驱动
- String的indexOf()用于获取字符串中某个子字符串的位置
- Typescript学习笔记(四)class 类
- C语言复习---输出魔方阵
- 案例学python——案例一:抓图
- Fragment中onCreateView()和onActivityCreated():静态/动态View
- 2.11 alert\confirm\prompt
- CRM项目hellokitty部分交互界面
- pgm1
- tips: javascript 参数传递含有空格怎么办?
- git 删除 repository
- 用js获取当前月份的天数
- 每天学一点easyui①
- HDU-4336-期望dp-bit
- android studio的jni和so
- sendcloud golang 发送短信 示例代码
热门文章
- PHP高手干货分享:要大大提高PHP效率
- navicat重新系统丢失libmysql_e
- Tomcat 的context.xml
- ASP.NET之电子商务系统开发-4(二级分类)
- C语言:类似linux内核的分等级DEBUG宏(打印宏)
- cocos2d-x3.9利用cocos引擎一键打包Android平台APK(C++小白教程)
- Java学习之equals和==的区别
- JavaScript 运动框架 Step by step
- [LeetCode]题解(python):073-Set Matrix Zeroes
- python自学笔记(四)python基本数据类型之元组、集合、字典