js判断设备类型

js判断设备类型:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>responsive demo</title>
  <script src="device.js"></script>
</head>
<body style="margin: auto; position: absolute; width:100%; height: 100%">
<script>
      var isMobile = device.mobile(),
            isTable = device.tablet();

       if(isMobile || isTable){
             window.open("m.html","_self");
       } else{
         window.open("pc.html","_self");
       }
</script>
</body>
</html>

当然,也可以用device.js来逐个检测设备。

javascript方法如下:

Device JavaScript Method
Mobile device.mobile()
Tablet device.tablet()
iOS device.ios()
iPad device.ipad()
iPhone device.iphone()
iPod device.ipod()
Android device.android()
Android Phone device.androidPhone()
Android Tablet device.androidTablet()
BlackBerry device.blackberry()
BlackBerry Phone device.blackberryPhone()
BlackBerry Tablet device.blackberryTablet()
Windows device.windows()
Windows Phone device.windowsPhone()
Windows Tablet device.windowsTablet()
Firefox OS device.fxos()
Firefox OS Phone device.fxosPhone()
Firefox OS Tablet device.fxosTablet()
MeeGo device.meego()

比如可以用如下代码来检测设备是否为IOS设备

var isIPhone = device.iphone(),

      isIPad = device.ipad();

var isIOS = isIPhone  || isIPad;

if(isIOS){

        alert("is this iOS?"+isIOS);

}

或者可以用来控制当为mobile或者tablet的时候加载m.css, PC的时候加载pc.css

if(isMobile | isTable){

        document.write( ' <link rel="stylesheet" href="m.css">');

}else{

        document.write('<link rel="stylesheet" href="pc.css">');

}

参考链接:

https://github.com/matthewhudson/device.js

猜你喜欢

转载自guwq2014.iteye.com/blog/2275222