2012-08-13 167 views
9

是否有方法使用javascript获取移动设备的名称(例如“John的iPhone”)?从javascript获取(移动)设备名称


也许我不是很清楚......我的意思是不是无论是在iPhone,iPad等,但在“设备名称” - 例如,它可以是“约翰的iPhone”。

+0

取决于移动设备所运行的浏览器,但你要知道,如果它是一个iPhone或Android设备或者你究竟需要哪个名字? – 2012-08-13 08:13:40

+0

唯一可以得到的是User-Agent – 2012-08-13 08:19:42

+0

这是一个使用'WURFL.js'的链接,免费http://www.smashingmagazine.com/2014/07/01/server-side-device-detection- with-javascript/ – Pierre 2015-03-11 06:53:30

回答

10

你不能在本地浏览器中运行一个Web应用程序做到这一点通过JavaScript - JavaScript的一般不具有访问自己的个人识别数据。

一种可能的方式是使用可能有API访问设备名称的框架,如PhoneGap。但是,您只能通过应用商店部署您的网站,所以根据您的使用情况,这可能会非常有限。

10

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

例如,

var ua = navigator.userAgent, 
browser = { 
    iPad: /iPad/.test(ua), 
    iPhone: /iPhone/.test(ua), 
    Android4: /Android 4/.test(ua) 
}; 

这会给你访问之类的东西if(browser.iPad) { /* do stuff */ }

+1

一个很好的简洁和可扩展的答案,谢谢。 'device'将是变量的一个更合适的名称。 – Rebs 2016-08-08 01:40:38

1

我正在使用嵌入式扫描仪的移动设备。为了能够使用一些不同设备的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(); 
}, 

你可以说感谢科里LaViska。我根据他的工作做到了这一点。这里是链接,如果你想了解更多

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