如何快速检测用户代理是否为 metro UI 版本的 internet-explorer >=10
?
如何快速检测用户代理是否为 metro UI 版本的 internet-explorer >=10
?
所以,似乎没有明确的测试方法可以区分Metro IE和桌面IE,但似乎有一些不同的数据可以尝试使用以假设它是Metro。不幸的是,我发现的所有项都可以通过其他设置来解释掉。换句话说,对于我发现的所有“特征”测试,桌面IE都可以被配置成以欺骗测试认为它正在运行Metro。
ActiveX是否已禁用(Metro不允许任何ActiveX内容,但桌面IE也可以将其设置为禁用):
function isActivexEnabled() {
var supported = null;
try {
supported = !!new ActiveXObject("htmlfile");
} catch (e) {
supported = false;
}
return supported;
}
用户代理字符串检查(Metro总是以64位模式运行,但在32位机器上不会运行,并且桌面IE也可以配置为以64位模式运行,但不确定这些选项有多受欢迎)
function isWin64() {
return navigator.platform == "Win64";
}
全屏检查(Metro模式始终处于全屏模式,但是桌面版IE也可以运行在全屏模式下,这可能作为支持Metro模式的证据)
function isFullScreen() {
return (window.innerWidth == screen.width &&
window.innerHeight == screen.height);
}
简而言之,我认为您必须尝试检查一堆功能,然后猜测,没有明确的方法。或者您可以接受微软不希望您这样做,并针对您想使用的功能使用功能检测。window.innerHeight
和window.innerWidth
都不能与屏幕高度/宽度进行比较,因为它们不是以物理像素为单位测量的。innerHeight/innerWidth值与窗口缩放成反比。 - robocatdocument.documentElement.clientWidth == screen.width
,因为在Metro上,由于Metro中的一个错误,zoom不会改变clientWidth值。 在桌面上,根据缩放比例,该值会更改(正确的行为)。但是,如果用户在桌面IE10上按F11,则嗅探功能将无法正常工作。 请尝试以下链接:http://jsbin.com/iqibik/2(测试窗口/文档变量时必须使用jsbin而非嵌入式jsfiddle!) - robocat我不相信有一种方法可以确定请求是来自IE10的Metro版本还是常规桌面模式版本。 Metro IE没有唯一的HTTP标头或用户代理字符串可用于识别它。
Windows 8 64位上的Internet Explorer 10用户代理字符串
Metro IE10是一个64位应用程序,因此您将在用户代理字符串中看到“Win64”。默认情况下,常规桌面IE10为32位并显示“WOW64”。基于这个你可能会作出一个大致的判断,但你会错误地识别选择在桌面模式下运行64位版本的IE10的任何人。从该表中可以看出,用户代理字符串是相同的。[编辑:如评论中所述,这对32位PC无效。]
我能理解你想要检测Metro IE的原因,但这种“浏览器嗅探”通常不被鼓励,因为它很繁琐且容易出错。如果可以的话,最好使用类似Modernizr的东西对所需的特定浏览器功能进行功能测试。var IEmetrotest = ((cssua.userAgent.ie > 9) && (metrotest()));
yepnope({
test: IEmetrotest,
yep : ['ie_metro.css']
});
我在Browserstack中测试了它,结果符合预期。(我本想点赞你的,但是还没有足够的积分。)
// Minor revisions to isBrowserSupportPlugin() previously posted
// Renamed function to metrotest() and inverted the returned value.
var errorName = null;
function metrotest() {
var supported = null;
try {
new ActiveXObject("");
}
catch (e) {
// FF has ReferenceError here
errorName = e.name;
}
try {
supported = !!new ActiveXObject("htmlfile");
} catch (e) {
supported = false;
}
if(errorName != 'ReferenceError' && supported==false){
supported = false;
}else{
supported =true;
}
return !supported;
}
这里提供一种一致的方法,不依赖于ActiveX检查,在IE10和IE11中经过测试。
if (window.screenY === 0 && (window.innerHeight+1) !== window.outerHeight){ //IE metro/modern UI }
outerHeight
和innerHeight
之间的1像素差异。window.screenX === 0
。只使用screenY
以覆盖窗口上下文捕捉到右侧的Modern/Metro快照模式(即screenX!== 0)。!!window.StyleMedia
,考虑到IE 10、11和即将推出的IE12之间的根本用户代理更改。我最终会相应地更新我的答案。 - hexalysMETRO IE不支持ActiveX或任何插件对象。基于此,以下脚本已经测试并且运行良好。
//---------------------------Metro IE check-------------------------
var errorName = null;
function isBrowserSupportPlugin() {
var supported = null;
try {
new ActiveXObject("");
}
catch (e) {
// FF has ReferenceError here
errorName = e.name;
}
try {
supported = !!new ActiveXObject("htmlfile");
} catch (e) {
supported = false;
}
if(errorName != 'ReferenceError' && supported==false){
supported = false;
}else{
supported =true;
}
return supported;
}
//----------------------------------------------------------------
可靠的现代/ Metro 检测!
我找到了一种检测现代和桌面模式的方法,您可以在此处尝试:http://jsbin.com/genac/2(使用 Chrome 或 Firefox 进行编辑,而不是 IE http://jsbin.com/genac/2/edit)。
它似乎是一种相当强大的检测方式,因为:
诀窍在于,现代页面上的滚动条不会使客户端窗口大小变小(它们出现在页面上方),但桌面页面上的滚动条会影响客户端窗口大小(可用窗口大小更小)。
主要缺点是测试需要页面上有滚动条才能探测差异。在 iframe 或自动滚动的 div 中是否会出现相同的滚动条差异?
编辑:在使用此代码之前,我建议先测试浏览器是否为IE11,因为Windows 10没有Metro / Modern界面,而Edge表现略有不同。<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Metro width detector - robocat</title>
<script>
function show() {
var div = document.createElement('div');
var s = [];
s.push('IS METRO? ' + ((tester.offsetWidth == window.outerWidth) ? 'YES' : 'NO'));
s.push('document.documentElement.clientWidth: ' + document.documentElement.clientWidth);
s.push('document.documentElement.offsetWidth: ' + document.documentElement.offsetWidth);
s.push('window.innerWidth: ' + window.innerWidth);
s.push('window.outerWidth: ' + window.outerWidth);
s.push('screen.width: ' + screen.width);
s.push('screen.availWidth: ' + screen.availWidth);
s.push('document.documentElement.getBoundingClientRect().width: ' + document.documentElement.getBoundingClientRect().width);
s.push('tester.offsetWidth: ' + tester.offsetWidth);
div.innerHTML = s.join('<br>');
document.body.insertBefore(div, document.body.firstChild);
}
window.onresize = function() {
show();
}
</script>
</head>
<body onload="show()" style="margin:0">
<div id="tester" style="position:absolute;left:0;right:0;"></div>
<div style="height:10000px;width:10px;background-color:blue;"></div>
</body>
</html>
function doChecks_Browser() {
// full pin abilities == can pin && Win7+
if (pinnedSiteDetection.hasFullPinAbilityBrowser)
}
关于此事的更多信息在这里。
MSDN上的这篇文章中提到...
Windows 8 Release Preview通过在开始屏幕上使用磁贴来实现固定站点。当用户单击固定站点的磁贴时,该站点将在Windows Internet Explorer 10 Release Preview中以Windows Metro样式UI环境打开。
希望这可以帮助到您!
var _ua = window.navigator.userAgent;
var isIDevice = (/iphone|ipod|ipad/i).test(_ua);
var isMobileChrome = (_ua.indexOf('Android') > -1 && (/Chrome\/[.0-9]*/).test(_ua) && _ua.indexOf("Version") == -1);
var isMobileIE = _ua.indexOf('Windows Phone') > -1;
function isActivexEnabled()
{
var supported = null;
try
{
supported = !!new ActiveXObject("htmlfile");
}
catch (e)
{
supported = false;
}
return supported;
}
if (!isIDevice && !isMobileChrome && !isMobileIE && _ua.toLowerCase().indexOf("wow") == -1 && !isActivexEnabled())
{
//IE Metro UI
}
这对我有用。
我尝试了下面的代码,看起来是可以工作的。
但是如果用户在Internet选项->自定义级别->将ScriptActivexControlsMarkedSafeForScripting设置为false,它就无法捕获。如果用户这样做,代码会认为它是Metro/Modern界面。
目前我没有找到绕过这个问题的方法,比如检查操作系统、全屏模式之类的。
isWinModern用于检查Metro/Modern界面。
isWinDesktop用于检查Windows桌面(=非modern界面)。
以下代码不能保证能够正常工作。
function isWin8Modern() {
if (!!window.ActiveXObject) {
try {
!!new window.ActiveXObject('htmlfile');
return false;
} catch (exc) {
return true;
}
} else {
return false;
}
}
function isWinDesktop() {
if (window.ActiveXObject) {
try {
new window.ActiveXObject('htmlfile');
return true;
} catch (exc) {
return false;
}
} else {
return false;
}
}
这可能有点冒险,但似乎是一个相当明智的解决方案:
为什么不检查浏览器是否全屏1,并根据此调用全屏元素metro。我在工作电脑上运行着Windows 8,所以明天我会尝试检查是否还有任何GUI(我不认为我记得有任何GUI),如果有,您需要手动减去它们。确实,这不是最美丽的解决方案,但据我所知,不会有任何“美丽”的解决方案,这可能证明是一个相当可靠的黑客,因为metro IE的GUI不能使用任何工具栏或类似软件更改(据我所知)。确实,这可能会导致桌面IE的错误识别,但即使如此,全屏桌面IE仍将提供类似的体验。
1 给出类似以下的内容:
if(window.innerWidth == screen.width && window.innerHeight == screen.height) {
// metro
} else {
// desktop
}