JavaScript判断移动端及pc端访问不同的网站

来源:互联网 发布:淘宝宝贝照片拍摄 编辑:程序博客网 时间:2024/06/03 22:00
假如我们有一个网站,pc端通过www.test.com访问,而移动端通过m.test.com来访问。我们需要做的就是当移动端访问www.test.com时可以直接跳转到m.test.com。此时我们只需这样处理就可以了,在页面头部加入如下js代码:

(function () {
    var url = location.href;
    // replace www.test.com with your domain
    if ( (url.indexOf('www.test.com') != -1) && navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i) ) {
        location.href = 'http://m.test.com';
    }
})();
但是,多数情况下不止这么简单地直接从www.test.com跳转到m.test.com。我们网站除了主机名部分,后面跟的还有,比如:www.test.com/list/98/,对于这样一个url,PC就直接这样访问了,对于移动端,需要通过m.test.com/list/98/才可以呈现出比较好的效果。

那么,此时就可以用正则来处理,当移动端访问时,我们把“http://www”替换为“http://m”(冒号为英文冒号),然后更新页面就可以看到页面在移动端上呈现的效果了。具体代码如下:

(function () {
    var url = location.href;
    // replace www.test.com with your domain
    if ( (url.indexOf('www.test.com') != -1) && navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i) ) {
        var newUrl = url.replace('http://www', 'http://m');
        location.href = newUrl;
    }
})();
阅读全文
0 0
原创粉丝点击