1. 程式人生 > >一個API解決 區分當前使用應用的一切裝置平臺(Android、IOS、微信、QQ等等一切有提供支援的)

一個API解決 區分當前使用應用的一切裝置平臺(Android、IOS、微信、QQ等等一切有提供支援的)

Window Navigator

示例:

<div id="example"></div>

<script>
var txt = '';
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";

document.getElementById("example").innerHTML=txt;

</script>

如何測試呢? 使用Chrome瀏覽器,F12調出除錯視窗,選中豎向三點-->More tools-->Network conditions

具體操作如下圖:

根據平臺去手動輸入不同平臺的使用者代理,例如我圖上所選的就是Android+微信的使用者代理去測試,在微信上點選我的應用提供的點選按鈕,彈出alert('當前是微信平臺'),就證明測試成功了。

測試程式碼具體如下:

<script type="text/javascript">
        window.onload = function() {
             isWeixinBrowser();
        }
        //判斷是否微信瀏覽器
        function isWeixinBrowser() {  
            var ua = navigator.userAgent.toLowerCase();  
            var result = (/micromessenger/.test(ua)) ? true : false;
            if (result) {
                alert('你正在訪問微信瀏覽器');
            }
            else {
                alert('你訪問的不是微信瀏覽器');
            }
            return result;
      }
  </script>