Javascript 从javascript获取(移动)设备名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11930356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting (mobile) device name from javascript
提问by julius_am
Is there a way to get the name of a mobile device (e.g. "John's iPhone" ) using javascript?
有没有办法使用 javascript 获取移动设备的名称(例如“John's iPhone”)?
Maybe I wasn't very clear... what I meant is not whether it's an iPhone, iPad etc. but the "device name" - for example it can be "John's iPhone".
也许我不是很清楚……我的意思不是它是 iPhone、iPad 等,而是“设备名称”——例如它可以是“约翰的 iPhone”。
采纳答案by Kevin
You can't do this through javascript for a web app running in a native browser - javascript generally doesn't have access to this personal identifying data.
对于在本机浏览器中运行的 Web 应用程序,您无法通过 javascript 执行此操作 - javascript 通常无权访问此个人识别数据。
One possible way is to use a framework like PhoneGapwhich may have an API to access the device name. But then, you can only deploy your web site via an app store, so this could be very limiting based on your use case.
一种可能的方法是使用像PhoneGap这样的框架,它可能有一个 API 来访问设备名称。但是,您只能通过应用商店部署您的网站,因此根据您的用例,这可能会非常有限。
回答by Adam Heath
Your best bet is to use the user agent:
最好的办法是使用用户代理:
e.g.
例如
var ua = navigator.userAgent,
browser = {
iPad: /iPad/.test(ua),
iPhone: /iPhone/.test(ua),
Android4: /Android 4/.test(ua)
};
This will give you access to things like if(browser.iPad) { /* do stuff */ }
这将使您可以访问诸如 if(browser.iPad) { /* do stuff */ }
回答by Damien1970
I'm working with mobile device with embedded scanner. To be able to use some the JavaScript library of different devices and to avoid conflict with those libraries of different manufacturer (Zebra, Honeywell, Datalogic, iOs ect...) I need to come up with a way to identify each devices so I can load the proper library and this is what I came up with. Enjoy
我正在使用带有嵌入式扫描仪的移动设备。为了能够使用不同设备的一些 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;
},
and this is an example of how it can be used:
这是如何使用它的示例:
initializeHandheldScanners: function () {
if (DeviceCtrl.getDeviceName() == 'Zebra')
DeviceCtrl.initializeSymbolScanner();
if (DeviceCtrl.getDeviceName() == 'Honeywell')
DeviceCtrl.initializeHoneywellScanner();
if (DeviceCtrl.getDeviceName() == 'Datalogic')
DeviceCtrl.initializeDatalogicScanner();
},
You can say thanks to Cory LaViska. I did this based on his work. Here is the link if you want to know more
您可以感谢 Cory LaViska。我是根据他的工作做的。如果你想了解更多,这里是链接
https://www.abeautifulsite.net/detecting-mobile-devices-with-javascript
https://www.abeautifulsite.net/detecting-mobile-devices-with-javascript
回答by Josh Merlino
You can use this snippet:
您可以使用此代码段:
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());