1. 程式人生 > >js判斷終端是移動端還是pc端

js判斷終端是移動端還是pc端

<!doctype html>
<html>
     <meta charset="utf-8" />
     <head>
           <title>web1</title>
     </head>
     <body>
     </body>
</html>
<script type="text/javascript">
     //平臺、裝置和作業系統
     var system = {
           win: false,
           mac: false,
           xll: false,
           ipad: false
     };
     //檢測平臺
     var p = navigator.platform;
     system.win = p.indexOf("Win") == 0;
     system.mac = p.indexOf("Mac") == 0;
     system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
     system.ipad = (navigator.userAgent.match(/iPad/i) != null) ? true : false;
     //跳轉語句,如果是PC訪問就自動跳轉到所要訪問的頁面http://www.電腦.com
     if(system.win || system.mac || system.xll || system.ipad) {
           //預設訪問http://www.電腦.com
           alert(11111)
     } else {
           alert(222)
     }
</script>