uaredirect.js 进行判断是pc还是wap端(移动端) wap端跳转页面

在pc的网址里面加入代码

<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script>

 <script type="text/javascript">uaredirect("http://wap.tongji021.com");</script>

第一个script  是为了引用百度的js代码

第二个script就是跳转到你想要跳转的wap页面


这样就能实现  wap访问之时   跳转到你想跳转的页面


---------------------------------------------分隔符--------------------------------

批量替换把www换成m


[html]  view plain  copy
  1.  function mobile_device_detect(url)  
  2. {  
  3. var thisOS=navigator.platform;  
  4. var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");  
  5. for(var i=0;i<os.length;i++)  
  6. {  
  7. if(thisOS.match(os[i]))  
  8. {   
  9. window.location=url;  
  10. }  
  11.   
  12. }  
  13. //因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认  
  14. if(navigator.platform.indexOf('iPad') != -1)  
  15. {  
  16. window.location=url;  
  17. }  
  18. //做这一部分是因为Android手机的内核也是Linux  
  19. //但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断  
  20. var check = navigator.appVersion;  
  21. if( check.match(/linux/i) )  
  22. {  
  23. //X11是UC浏览器的平台 ,如果有其他特殊浏览器也可以附加上条件  
  24. if(check.match(/mobile/i) || check.match(/X11/i))  
  25. {  
  26. window.location=url;  
  27. }   
  28. }  
  29. //类in_array函数  
  30. Array.prototype.in_array = function(e)  
  31. {  
  32. for(i=0;i<this.length;i++)  
  33. {  
  34. if(this[i] == e)  
  35. return true;  
  36. }  
  37. return false;  
  38. }  
  39. }   
  40. var url = window.location.href;  
  41. url = url.replace("www.", "m.");  
  42. mobile_device_detect(url);  



百度判断手机终端并自动跳转uaredirect.js代码及使用实例


百度siteapp下的一款跳转的产品,使用起来很方便。你可以用这款JS跳转到手机版,也可以跳转到任何你想跳转的位置。

js代码如下:

function uaredirect(f) {
    try {
        if (document.getElementById("bdmark") != null) {
            return
        }
        var b = false;
        if (arguments[1]) {
            var e = window.location.host;
            var a = window.location.href;
            if (isSubdomain(arguments[1], e) == 1) {
                f = f + "/#m/" + a;
                b = true
            } else {
                if (isSubdomain(arguments[1], e) == 2) {
                    f = f + "/#m/" + a;
                    b = true
                } else {
                    f = a;
                    b = false
                }
            }
        } else {
            b = true
        }
        if (b) {
            var c = window.location.hash;
            if (!c.match("fromapp")) {
                if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))) {
                    location.replace(f)
                }
            }
        }
    } catch(d) {}
}
function isSubdomain(c, d) {
    this.getdomain = function(f) {
        var e = f.indexOf("://");
        if (e > 0) {
            var h = f.substr(e + 3)
        } else {
            var h = f
        }
        var g = /^www\./;
        if (g.test(h)) {
            h = h.substr(4)
        }
        return h
    };
    if (c == d) {
        return 1
    } else {
        var c = this.getdomain(c);
        var b = this.getdomain(d);
        if (c == b) {
            return 1
        } else {
            c = c.replace(".", "\\.");
            var a = new RegExp("\\." + c + "$");
            if (b.match(a)) {
                return 2
            } else {
                return 0
            }
        }
    }
};



使用方法:

保存以上代码为uaredirect.js,将这个JS文件的引用最好放到网站的title下,不要放在最后或者其他部位,这样做是因为会先加载uaredirect.js后跳转,这样做会很快,如果先加载其他的JS或者CSS的话,会先加载它们再跳转,这样跳转速度就会很慢了。加载的话,只需要一段代码就OK了

引用代码:

<script src="你的js存放目录/uaredirect.js" type="text/javascript"></script>
<script type="text/javascript">uaredirect("http://m.haishiit.com","http://www.haishiit.com");</script>

说明:http://m.haishiit.com 是手机版的网站, http://www.haishiit.com 是电脑版的网站。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

随便起的名字也被占用

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值