移动端 UserAgent 不完全统计

今天处理一个时尚网的专题,页面的主体由客户自己开发,iframe 嵌入各个媒体,据说这种形式会给数据监测带来一些问题,对低版本的浏览器不够友好。

查看源码,发现有部分代码片断,可供我们学习和参考。

1、判断浏览器UA,如果发现是移动端访问,自动跳转至WAP:

function browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    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 (bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
        if(location.href.indexOf('?') > -1){
            location.href = './wap/'+ location.href.substring(location.href.lastIndexOf('?'),location.href.length);
        }else{
            location.href = './wap/';
        }
    }
}
(browserRedirect();

2、获取URL参数:

function getParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return '';
} 

Got Something To Say:

Your email address will not be published. Required fields are marked *

*

© 2006-2018. Powered by WordPress Romangie & Bootstrap. ICP No. 15008795