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

380 阅读1分钟

第一种

	<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>


\