標籤:js判斷行動裝置
方法一:純JS判斷
使用這方法既簡單,又實用,不需要引入jQuery庫,把以下代碼加入到<head>裡即可。
<script type=”text/javascript”>if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {window.location = “mobile.html”; //可以換成http地址}</script>
方法二:使用 Device.js 庫
device.js 是一個用於檢查裝置用的外掛程式,使用它你可以很方便的判斷裝置的作業系統,以及裝置是縱向還是橫向。
首先,我們下載Device.js
: https://github.com/matthewhudson/device.js
STEP 1: 引入 JS 檔案
<script src=”device.min.js”></script>
STEP 2: 加入判斷代碼
<script type=”text/javascript”>if(device.mobile()){window.location = “shouji.html”; //可以換成http地址}</script>
Device.js 方法有很多,若你想實現對某個裝置的判斷,要以根據以下代碼來替換device.mobile()。
650) this.width=650;" alt="JS判斷行動裝置最佳方法 並實現跳轉至手機版網頁" src="http://static.open-open.com/lib/uploadImg/20151204/20151204211230_444.png" width="500" height="854" style="border:0px;vertical-align:middle;" />
JS判斷行動裝置最佳方法 並實現跳轉至手機版網頁