【问题标题】:Getting (mobile) device name from javascript从javascript获取(移动)设备名称
【发布时间】:2012-08-09 10:47:03
【问题描述】:

有没有办法使用 javascript 获取移动设备的名称(例如“John's iPhone”)?


也许我不是很清楚......我的意思不是它是否是 iPhone、iPad 等,而是“设备名称”——例如它可以是“约翰的 iPhone”。

【问题讨论】:

  • 取决于移动设备运行的浏览器,但您想知道它是 iphone 还是 android 设备,或者您需要哪个名称?
  • 唯一能得到的就是User-Agent
  • 这里是WURFL.js的使用链接,免费smashingmagazine.com/2014/07/01/…

标签: javascript mobile-website


【解决方案1】:

最好的办法是使用用户代理:

例如

const ua = navigator.userAgent
const device = {
  iPad: /iPad/.test(ua),
  iPhone: /iPhone/.test(ua),
  Android4: /Android 4/.test(ua)
}

该对象将允许您编写漂亮的条件逻辑,例如if(device.iPad) { /* do stuff */ }

【讨论】:

    【解决方案2】:

    你可以使用这个sn-p:

    const getUA = () => {
        let device = "Unknown";
        const ua = {
            "Generic Linux": /Linux/i,
            "Android": /Android/i,
            "BlackBerry": /BlackBerry/i,
            "Bluebird": /EF500/i,
            "Chrome OS": /CrOS/i,
            "Datalogic": /DL-AXIS/i,
            "Honeywell": /CT50/i,
            "iPad": /iPad/i,
            "iPhone": /iPhone/i,
            "iPod": /iPod/i,
            "macOS": /Macintosh/i,
            "Windows": /IEMobile|Windows/i,
            "Zebra": /TC70|TC55/i,
        }
        Object.keys(ua).map(v => navigator.userAgent.match(ua[v]) && (device = v));
        return device;
    }
    
    console.log(getUA());
    

    【讨论】:

      【解决方案3】:

      我正在使用带有嵌入式扫描仪的移动设备。为了能够使用一些不同设备的 JavaScript 库并避免与不同制造商(Zebra、Honeywell、Datalogic、iOs 等)的那些库发生冲突,我需要想出一种方法来识别每个设备,这样我就可以加载正确的库,这就是我想出的。享受

      getDeviceName: function () {
          var deviceName = '';
      
          var isMobile = {
              Android: function() {
                  return navigator.userAgent.match(/Android/i);
              },
              Datalogic: function() {
                  return navigator.userAgent.match(/DL-AXIS/i);
              },
              Bluebird: function() {
                  return navigator.userAgent.match(/EF500/i);
              },
              Honeywell: function() {
                  return navigator.userAgent.match(/CT50/i);
              },
              Zebra: function() {
                  return navigator.userAgent.match(/TC70|TC55/i);
              },
              BlackBerry: function() {
                  return navigator.userAgent.match(/BlackBerry/i);
              },
              iOS: function() {
                  return navigator.userAgent.match(/iPhone|iPad|iPod/i);
              },
              Windows: function() {
                  return navigator.userAgent.match(/IEMobile/i);
              },
              any: function() {
                  return (isMobile.Datalogic() || isMobile.Bluebird() || isMobile.Honeywell() || isMobile.Zebra() || isMobile.BlackBerry() || isMobile.Android() || isMobile.iOS() || isMobile.Windows());
              }
          };
      
          if (isMobile.Datalogic())
              deviceName = 'Datalogic';
          else if (isMobile.Bluebird())
              deviceName = 'Bluebird';
          else if (isMobile.Honeywell())
              deviceName = 'Honeywell';
          else if (isMobile.Zebra())
              deviceName = 'Zebra';
          else if (isMobile.BlackBerry())
              deviceName = 'BlackBerry';
          else if (isMobile.iOS())
              deviceName = 'iOS';
          else if ((deviceName == '') && (isMobile.Android()))
              deviceName = 'Android';
          else if ((deviceName == '') && (isMobile.Windows()))
              deviceName = 'Windows';
      
          if (deviceName != '') {
              consoleLog('Devices information deviceName = ' + deviceName);
              consoleLog('Devices information any = ' + isMobile.any());
              consoleLog('navigator.userAgent = ' + navigator.userAgent);
          }
      
          return deviceName;
      },
      

      这是一个如何使用它的示例:

      initializeHandheldScanners: function () {
          if (DeviceCtrl.getDeviceName() == 'Zebra')
              DeviceCtrl.initializeSymbolScanner();
      
          if (DeviceCtrl.getDeviceName() == 'Honeywell')
              DeviceCtrl.initializeHoneywellScanner();
      
          if (DeviceCtrl.getDeviceName() == 'Datalogic')
              DeviceCtrl.initializeDatalogicScanner();
      },
      

      您可以对 Cory LaViska 表示感谢。我是根据他的工作做的。如果你想了解更多,这里是链接

      https://www.abeautifulsite.net/detecting-mobile-devices-with-javascript

      【讨论】:

        【解决方案4】:

        对于在本机浏览器中运行的网络应用程序,您无法通过 javascript 执行此操作 - javascript 通常无法访问此个人识别数据。

        一种可能的方法是使用像PhoneGap 这样的框架,它可能有一个API 来访问设备名称。但是,您只能通过应用商店部署您的网站,因此根据您的用例,这可能会受到很大限制。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-04
          • 1970-01-01
          • 2018-04-14
          • 1970-01-01
          相关资源
          最近更新 更多