-->

从JavaScript获取(移动)设备名称(Getting (mobile) device name

2019-06-27 00:09发布

有没有办法让使用JavaScript的移动设备(例如“约翰的iPhone”)的名字?


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

Answer 1:

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

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



Answer 2:

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

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

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



Answer 3:

我与嵌入式扫描仪的移动设备工作。 为了能够使用不同设备的一些JavaScript库,避免与不同厂家的那些库(斑马,霍尼韦尔,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



文章来源: Getting (mobile) device name from javascript