-2

我写了一些简单的代码来查找设备的类型:

$(document).ready(detectDevice);

function detectDevice() {
    if (window.innerWidth > 768) {
        var device = "Desktop";

        console.log('=======device======', device);
    } else if (window.innerWidth < 767 && window.innerWidth > 481) {
        var device = "iPad";
    } else if (window.innerWidth < 480) {
        var device = "Mobile Phone";
    }
}

但我想找到正在使用该应用程序的设备的正确型号名称。我不想编写navigator.userAgent使用浏览器查找的代码。

请有人告诉我是否有任何方法可以使用 JavaScript 或 jQuery 找到它?

4

1 回答 1

0

您可以使用这个轻量级库来执行此操作

https://github.com/matthewhudson/current-device

下面是它的工作原理:

  console.log("device.portrait() === %s", device.portrait());
  console.log("device.landscape() === %s", device.landscape());
  console.log("device.mobile() === %s", device.mobile());
  console.log("device.tablet() === %s", device.tablet());
  console.log("device.ipad() === %s", device.ipad());
  console.log("device.ipod() === %s", device.ipod());
  console.log("device.iphone() === %s", device.iphone());
  console.log("device.android() === %s", device.android());
  console.log("device.androidTablet() === %s", device.androidTablet());
  console.log("device.blackberryTablet() === %s", device.blackberryTablet());
  console.log("device.fxos() === %s", device.fxos());
  console.log("device.fxosPhone() === %s", device.fxosPhone());
  console.log("device.fxosTablet() === %s", device.fxosTablet());

测试 Ipad

  if (device.ipad()) //....do something
于 2014-08-14T18:42:29.417 回答