腾讯首页分辨手机端与pc端代码

腾讯首页分辨手机端与pc端代码

  自己在做网页的时候在腾讯网首页借鉴的代码.

  代码:

 1 <!-- 移动适配JS脚本 -->
 2     <script type="text/javascript">
 3         if (window.location.toString().indexOf('pref=padindex') != -1) {
 4         } else {
 5             if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || /\(Android.*Mobile.+\).+Gecko.+Firefox/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))) {
 6                 if (window.location.href.indexOf("?mobile")<0){
 7                     try {
 8                         if (/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
 9                             window.location.href="https://www.xiyixu.top/mobile_index.html";
10                         } else if (/iPad/i.test(navigator.userAgent)) {
11                             //window.location.href=""
12                         } else {
13                             window.location.href="https://www.xiyixu.top/simple_index.html"
14                         }
15                     } catch (e) {}
16                 }
17             }
18         }
19     </script><!--[if !IE]>|xGv00|6a478dd274c352466ec1a24404f6e978<![endif]-->

见解:

if (/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent))
此句判断打开网页的设备是否为Android|Windows Phone|webOS|iPhone|iPod|这些设备,如果是,则指向一个新的网页,如果不是则继续执行.

代码源自腾讯网首页

2018年10月9日18:7:16

posted @ 2018-10-09 18:12  凌雲  阅读(317)  评论(0编辑  收藏  举报