方法一:純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()。
全文:http://www.open-open.com/lib/view/open1449234779436.html //jquery 判斷移動設備
附:
device.js cdn地址
http://www.bootcdn.cn/device.js/
附送:Jquery 判斷是不是 移動設備 瀏覽
首先,只判斷是否是用 移動設備 瀏覽的:
// Mobile 這里是只有不再 移動設備 上訪問時,才給相應元素加上 mouseenter 和 mouseleave 事件。
if (!navigator.userAgent.match(/mobile/i)) {
全文:http://blog.sina.com.cn/s/blog_471c84190102uzox.html
