使用device.js检测设备并实现不同设备展示不同网页

现在很多时候会用@media来控制页面在不同分辨率的设备商展示不同效果,但是有些时候想在直接在PC上展示一个做好的页面,在mobile展示另一个页面。这个时候可以借助device.js来检测设备,然后打开不同的页面(device.js 是一个可以用来检测设备,操作系统和方向的js库)。当然这种做法需要一次跳转。

假设有个m.html想用于mobile端展示,pc.html想用于pc端展示。这个时候可以用index.html作为入口,在<head>内引入device.js来检测设备,然后用js来实现跳转。

当设备为Mobile和tablet的时候展示m.html

否则展示pc.html

<!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代码如下

(function() {
  var previousDevice, _addClass, _doc_element, _find, _handleOrientation, _hasClass, _orientation_event, _removeClass, _supports_orientation, _user_agent;

  previousDevice = window.device;

  window.device = {};

  _doc_element = window.document.documentElement;

  _user_agent = window.navigator.userAgent.toLowerCase();

  device.ios = function() {
    return device.iphone() || device.ipod() || device.ipad();
  };

  device.iphone = function() {
    return _find(‘iphone‘);
  };

  device.ipod = function() {
    return _find(‘ipod‘);
  };

  device.ipad = function() {
    return _find(‘ipad‘);
  };

  device.android = function() {
    return _find(‘android‘);
  };

  device.androidPhone = function() {
    return device.android() && _find(‘mobile‘);
  };

  device.androidTablet = function() {
    return device.android() && !_find(‘mobile‘);
  };

  device.blackberry = function() {
    return _find(‘blackberry‘) || _find(‘bb10‘) || _find(‘rim‘);
  };

  device.blackberryPhone = function() {
    return device.blackberry() && !_find(‘tablet‘);
  };

  device.blackberryTablet = function() {
    return device.blackberry() && _find(‘tablet‘);
  };

  device.windows = function() {
    return _find(‘windows‘);
  };

  device.windowsPhone = function() {
    return device.windows() && _find(‘phone‘);
  };

  device.windowsTablet = function() {
    return device.windows() && _find(‘touch‘);
  };

  device.fxos = function() {
    return (_find(‘(mobile;‘) || _find(‘(tablet;‘)) && _find(‘; rv:‘);
  };

  device.fxosPhone = function() {
    return device.fxos() && _find(‘mobile‘);
  };

  device.fxosTablet = function() {
    return device.fxos() && _find(‘tablet‘);
  };

  device.meego = function() {
    return _find(‘meego‘);
  };

  device.mobile = function() {
    return device.androidPhone() || device.iphone() || device.ipod() || device.windowsPhone() || device.blackberryPhone() || device.fxosPhone() || device.meego();
  };

  device.tablet = function() {
    return device.ipad() || device.androidTablet() || device.blackberryTablet() || device.windowsTablet() || device.fxosTablet();
  };

  device.portrait = function() {
    return Math.abs(window.orientation) !== 90;
  };

  device.landscape = function() {
    return Math.abs(window.orientation) === 90;
  };

  device.noConflict = function() {
    window.device = previousDevice;
    return this;
  };

  _find = function(needle) {
    return _user_agent.indexOf(needle) !== -1;
  };

  _hasClass = function(class_name) {
    var regex;
    regex = new RegExp(class_name, ‘i‘);
    return _doc_element.className.match(regex);
  };

  _addClass = function(class_name) {
    if (!_hasClass(class_name)) {
      return _doc_element.className += " " + class_name;
    }
  };

  _removeClass = function(class_name) {
    if (_hasClass(class_name)) {
      return _doc_element.className = _doc_element.className.replace(class_name, "");
    }
  };

  if (device.ios()) {
    if (device.ipad()) {
      _addClass("ios ipad tablet");
    } else if (device.iphone()) {
      _addClass("ios iphone mobile");
    } else if (device.ipod()) {
      _addClass("ios ipod mobile");
    }
  } else if (device.android()) {
    if (device.androidTablet()) {
      _addClass("android tablet");
    } else {
      _addClass("android mobile");
    }
  } else if (device.blackberry()) {
    if (device.blackberryTablet()) {
      _addClass("blackberry tablet");
    } else {
      _addClass("blackberry mobile");
    }
  } else if (device.windows()) {
    if (device.windowsTablet()) {
      _addClass("windows tablet");
    } else if (device.windowsPhone()) {
      _addClass("windows mobile");
    } else {
      _addClass("desktop");
    }
  } else if (device.fxos()) {
    if (device.fxosTablet()) {
      _addClass("fxos tablet");
    } else {
      _addClass("fxos mobile");
    }
  } else if (device.meego()) {
    _addClass("meego mobile");
  } else {
    _addClass("desktop");
  }

  _handleOrientation = function() {
    if (device.landscape()) {
      _removeClass("portrait");
      return _addClass("landscape");
    } else {
      _removeClass("landscape");
      return _addClass("portrait");
    }
  };

  _supports_orientation = "onorientationchange" in window;

  _orientation_event = _supports_orientation ? "orientationchange" : "resize";

  if (window.addEventListener) {
    window.addEventListener(_orientation_event, _handleOrientation, false);
  } else if (window.attachEvent) {
    window.attachEvent(_orientation_event, _handleOrientation);
  } else {
    window[_orientation_event] = _handleOrientation;
  }

  _handleOrientation();

}).call(this);

当然,也可以用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">‘);
    }
时间: 2024-07-30 14:42:49

使用device.js检测设备并实现不同设备展示不同网页的相关文章

Device.js——检测设备平台、操作系统的Javascript 库

http://segmentfault.com/a/1190000000373735 Device.js 是一个可以让你检测设备的平台,操作系统和方向 JavaScript 库,它会自动在 <html> 标签添加一些设备平台,操作系统,方向相关的 CSS class,这样就能让你针对不同设备撰写不同的 CSS,并且还提供一些 Javascript 函数来判断设备. Device.js 通过操作系统(比如 iOS,安卓,黑莓,Windows,Firefox OX),方向(横屏或者竖屏),类型(平

JS检测移动端横竖屏的代码

这篇文章主要介绍了JS检测移动端横竖屏的代码,需要的朋友可以参考一下 使用media来判断屏幕宽度遇到的问题: ios上当我旋转屏幕的时候可行,但是安卓机上没反应,横屏显示的还是我竖屏的样式. 查了一下资料,css3的media如果要在移动端有较好的显示效果,需要在页头加上这段代码 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-s

JS 判断ipad android 等移动设备横竖屏代码 源码--AangJava

废话不多说,直接进入正题! <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <title> JS 判断ipad android 等移动设备横竖屏代码 源码--AangJava</

通过JS检测360浏览器

如何通过JS检测360浏览器? 尝试了一大堆方法,网上大多数办法都是通过navigator.userAgent来判断,这可能在几年前是行得通的,现在360userAgent输出来跟谷歌除了版本号其余一模一样... 谷歌:      Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 360极速:Mozilla/5.0 (Windo

如何用js检测判断时间日期的间距

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>如何用js检测判断日期大于多少天</title> <script> window.onload=function(){ var ul1=document.getElementById("time"); var span1=ul1.getElementsByTag

通过js检测到iframe,使父窗口重定向到index -----------???----------------------

通过js检测到iframe,使父窗口重定向到index -----------???---------------------- 如果本身已将在iframe中,那么重定向的页面应该直接添加到父级iframe中-----???----

js检测服务器端Seesion是否存在

<script> window.onload = function () { var s_uname = '<% =Session["uname"] %>'; if (s_uname == "") { window.location.href = "../Login.aspx"; } } </script> js检测服务器端Seesion是否存在

Device namespace简介 - 基于Kernel namespace的设备虚拟化

在移动设备上,虚拟化的需求正在逐渐增加.其一,移动设备配置越来越高,一些高端配置已和桌面设备接近,这为虚拟化奠定了基础:其二,用户对于移动设备使用场景的多样性与日俱增.现在移动设备不仅用于娱乐日用,还用于工作:其三,安全与隐私问题日益凸显.移动设备上有更多的隐私信息,如各种账号,支付密码等,同时,各种病毒木马正在向移动设备迅速蔓延.这种背景下在一个隔离的环境中运行敏感软件是更加安全的做法:其四,多用户需求的出现.有时手机,尤其平板用户是多个,比如给小孩玩时就希望在一个特定的受限运行环境下. 桌面

Js检测是否是360浏览器

Js检测用户浏览器是否是360浏览器,并以Alert弹出框的形式告之用户,当然在实际的使用中,并不需要弹出框,这个就根据需要自己修改代码吧. <html> <head>Js检测是否是360浏览器</head> <body> <script> if(window.external&&window.external.twGetRunPath){ var r=external.twGetRunPath(); if(r&&