js識別安卓和ios及pc端,以及解決微信的屏蔽

來源:互聯網
上載者:User

js識別安卓和ios及pc端,以及解決的屏蔽

這個功能其實主要針對下載功能來說的,

安卓肯定和ios不一樣,

如何在不同裝置訪問時就能根據使用者的裝置進行相應的跳轉。

同時,訪問的地址如果是的話,本身是會屏蔽跳轉的。

需要識別,並給出提示用其他瀏覽器開啟此網址。

代碼很簡單。完整代碼如下:

 1  2 
345611 <script>12 function detectOS() {13 var osUserAgent = navigator.userAgent.toLowerCase();14 if (osUserAgent.indexOf("iphone") > -1 || osUserAgent.indexOf("ipad") > -1 || osUserAgent.indexOf("ipod") > -1) {15 if (osUserAgent.indexOf("micromessenger") > -1) {16 return "wechat";17 }18 //alert("注意:只限越獄使用者可安裝");19 top.location.href = 'http://www.apple.com/';20 return "iphone";21 }22 if (osUserAgent.indexOf("android") > -1 || osUserAgent.indexOf("adr") > -1) {23 if (osUserAgent.indexOf("micromessenger") > -1) {24 return "wechat";25 }26 top.location.href = 'http://www.baidu.com';27 return "android";28 }29 if (osUserAgent.indexOf("win") > -1) {30 top.location.href = 'http://www.baidu.com';31 return "windows";32 }33 return osUserAgent;34 }35 detectOS();36 </script>383940

4142

三個if語句實現查詢:ios、Android、還是win(pc)  ,

在 ios和Android的語句中加入了

 if (osUserAgent.indexOf("micromessenger") > -1) {                   return "wechat";                }驗證是否是在中開啟的,如果不需要此功能可以刪去。
top.location.href = 'http://www.apple.com/';即為相應裝置的跳轉地址。最後 body裡面有一張圖片,當為訪問時,顯示此圖片,圖片內容為提示用其他瀏覽器開啟。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.