js判断是否是移动设备,并跳转

第一种

	<script type="text/javascript">
try {
var urlhash = window.location.hash;
if (!urlhash.match("fromapp"))
{
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i)))
{

	window.location='http://m.baidu.com';
}
}
}
catch(err)
{
}
</script>


第二种

/*检测浏览器方法
------------------------------------------------*/
var pageurl = window.location.search;
if (pageurl == '?m2w') {
    addCookie('m2wcookie', '1', 0);
}
if (getCookie('m2wcookie') != '1' && browserRedirect()) {
    location.href = 'http://www.hacnn.com';
}
///*工具类方法
//------------------------------------------------*/
//检测是否移动设备来访
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) {
        return true;
    } else {
        return false;
    }
}

//写Cookie
function addCookie(objName, objValue, objHours) {
    var str = objName + "=" + escape(objValue);
    if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失
        var date = new Date();
        var ms = objHours * 3600 * 1000;
        date.setTime(date.getTime() + ms);
        str += "; expires=" + date.toGMTString();
    }
    document.cookie = str;
}

//读Cookie
function getCookie(objName) {//获取指定名称的cookie的值
    var arrStr = document.cookie.split("; ");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] == objName) return unescape(temp[1]);
    }
    return "";
}

感觉cookie存值完全没有必要,不知道为什么原作者要这个写!




延伸阅读:

JS读取UserAgent信息并做判断  

userAgent信息可以由navigator.userAgent拿到。
例子:

<script type="text/javascript">
document.writeln("navigator.userAgent: " + navigator.userAgent + "<br />");
document.writeln("navigator.appName: " + navigator.appName + "<br />");
document.writeln("navigator.appCodeName: " + navigator.appCodeName + "<br />");
document.writeln("navigator.appVersion: " + navigator.appVersion + "<br />");
document.writeln("navigator.appMinorVersion: " + navigator.appMinorVersion + "<br />");
document.writeln("navigator.platform: " + navigator.platform + "<br />");
document.writeln("navigator.cookieEnabled: " + navigator.cookieEnabled + "<br />");
document.writeln("navigator.onLine: " + navigator.onLine + "<br />");
document.writeln("navigator.userLanguage: " + navigator.userLanguage + "<br />");
document.writeln("navigator.mimeTypes[1].description: " + navigator.mimeTypes[1].description + "<br />");
document.writeln("navigator.mimeTypes[1].type: " + navigator.mimeTypes[1].type + "<br />");
document.writeln("navigator.plugins[3].description: " + navigator.plugins[3].description + "<br />");
</script> 


如果想来判断其中内容,可以用
navigator.userAgent.match()或navigator.userAgent.indexOf()来判断
前者一般是用来判断手机客户端,例如navigator.userAgent.match(/iPhone/i) , navigator.userAgent.match(/Safari/i)
后者用来变向判断userAgent字符串中某值是否存在,例如 navigator.userAgent.indexOf("iPhone OS 4_0_2") !=-1


<script type="text/javascript">
var useHTML5 = 1;
if(navigator.userAgent.indexOf("iPhone OS 4_0_2") !=-1)
{
 useHTML5=2;
}
document.write("Value is: <br>");
document.write(useHTML5);
</script> 

所以它经常会用来判断浏览器类型,如navigator.userAgent.indexOf("Firefox")>0



番外

<script type="text/javascript">
if (window.location.toString().indexOf('pref=padindex') != -1) {} else {
    if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))) {
      if (window.location.href.indexOf("?mobile") < 0) {
        try {
          if (/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
            window.location.href = "/m/index.php";
          } else if (/iPad/i.test(navigator.userAgent)) {} else {}
        } catch(e) {}
      }
    }
  }
</script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙-极纪元JJY.Cheng

客官,1分钱也是爱,给个赏钱吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值