【问题标题】:Updating device detection code for Windows 8 mobile devices - phones and Surface X更新 Windows 8 移动设备(手机和 Surface X)的设备检测代码
【发布时间】:2014-12-17 01:13:37
【问题描述】:

我之前曾使用随附的代码检测移动设备,但它不适用于 Windows 8 手机和任何 Surface 版本(原因很明显)。我不知道适用于 Windows 8 手机和 Surface 的正确 userAgent。为了能够检测到 Surface 或 Windows 8 手机,需要进行哪些更改?任何帮助将不胜感激 !非常感谢。

function detect() {
var uagent = navigator.userAgent.toLowerCase();
var mobile = false;
var search_strings = [
"iphone",
"ipod",
"ipad",
"series60",
"windows ce",
"windows7phone",
"w7p",
"windows8phone",
"w8p",
"blackberry","
];
for (i in search_strings) {
    if (uagent.search(search_strings[i]) > -1)
        mobile = true;
    }
            return mobile;
}

if (detect()){
    window.location = "mobile";
}

【问题讨论】:

    标签: windows-phone-8 mobile device-detection


    【解决方案1】:

    User Agent 中使用RegExp 进行搜索,这涵盖了99% 的现代设备:

    /Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Symbian|Opera\sM(obi|ini)|Blazer|Dolfin|Dolphin|UCBrowser/.test(navigator.userAgent);
    

    您也可以使用服务wurfl.io - 它会在 javascript 中返回一个带有标志的对象是否是设备移动设备。它使用 wurfl 云和数千个已知的 UA。

    【讨论】:

    • 感谢您的即时回复 :-) wurfl.io 看起来像是添加所有新设备类型的方式,例如智能手表和智能电视等。但有一个问题 - 如果客户端浏览器关闭了 Javascript - 特别是在 Microsoft 设备上,那么这将不起作用,对吧?您可以推荐第二道防线吗?
    • 我也是为 Windows Phone 8+ 开发的。但我不知道 Mobile IE 中禁用的 javascript。里面没有这样的选项。另请注意wurfl.io 使用 javascript,并在窗口范围内创建一个对象 - WURFL,如果禁用 javascript,您的代码将不会运行,也不会使用WURLF
    • 好吧,我是 Windows mobile 编码的新手,所以我想澄清一下。非常感谢您的出色回答并解决了我的挑战。节日快乐!
    猜你喜欢
    • 2012-10-11
    • 2010-11-20
    • 2012-08-26
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 2022-01-03
    相关资源
    最近更新 更多