方法一:純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()。