JS判斷當前裝置是 PC IOS Andriod

wait.發表於2018-10-29

JS判斷當前裝置是 PC IOS Andriod 程式碼如下:

<script >

    window.onload = function(){

         var isPc = IsPC();
         var isAndroid = is_android();
         if (isPc){
         &emsp;&emsp;//pc
         &emsp;&emsp;location.href = url;
         }else{
         &emsp;&emsp;if (isAndroid){
        &emsp;&emsp;&emsp;&emsp; //安卓
        &emsp;&emsp;&emsp;&emsp; location.href = "";
        &emsp;&emsp; }else{
        &emsp;&emsp;&emsp;&emsp; //IOS
         &emsp;&emsp;&emsp;&emsp;location.href = "";

         &emsp;&emsp;}//前端全棧學習交流圈:866109386
         } // 面向1-3年前端開發人員
           // 幫助突破技術瓶頸,提升思維能力,歡迎大家。

         }



         //如果返回的是false說明當前作業系統是手機端,如果返回的是true則說明當前的作業系統是電腦端
         function IsPC() {
         var userAgentInfo = navigator.userAgent;
         var Agents = ["Android", "iPhone","SymbianOS", "Windows Phone","iPad", "iPod"];
         var flag = true;
         for (var v = 0; v < Agents.length; v++) {
      &emsp;&emsp;&emsp;&emsp;if (userAgentInfo.indexOf(Agents[v]) > 0) {
     &emsp;&emsp;&emsp;&emsp;&emsp;&emsp; flag = false;
     &emsp;&emsp;&emsp;&emsp;&emsp;&emsp; break;
      &emsp;&emsp;&emsp;&emsp;}
     &emsp;&emsp; }
     &emsp;&emsp; return flag;
      &emsp;&emsp;}

      //如果返回true 則說明是Android  false是ios
      function is_android() {
      var u = navigator.userAgent, app = navigator.appVersion;
      var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
      var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
      if (isAndroid) {
      &emsp;&emsp;//這個是安卓作業系統
      &emsp;&emsp;return true;
      }
      if (isIOS) {
      &emsp;&emsp;//這個是ios作業系統
     &emsp;&emsp; return false;
      }
      }


</script>
複製程式碼

相關文章