[英]How to detect Internet Explorer in JavaScript with Google Closure Compiler?
我有一个处理鼠标按钮事件的JavaScript函数。 它必须能够区分左右鼠标按钮。 遗憾的是,Internet Explorer对event.button使用的值与其他浏览器不同。 我知道如何解释它们,但我需要知道要走哪条路。
我使用依赖于条件编译的JavaScript hack来做到这一点。 就像这样:
if (/*@cc_on!@*/false) { IE fixup... }
我认为这是一种非常安全的方法,因为它基于JavaScript解析器功能,不能伪造,也不太可能被其他浏览器模仿。
现在我正在使用Google Closure Compiler打包我的JavaScript文件。 我发现它也像其他评论一样删除条件编译注释。 所以我尝试了不同的黑客。 其中一个是这样的:
if ("\v" == "v") { IE fixup... }
不幸的是,闭包编译器非常聪明,并发现条件永远不会为真并删除该代码。 此外,我不喜欢它,因为微软可能最终修复\\ v错误,然后检测失败。
我可以读取类似navigator.appName或者它所调用的内容,但这很容易伪造。 如果某人修改了他们的浏览器标识,他们就不太可能实现其他event.button行为......
Closure编译器允许保留某些注释。 我试过这个:
if (/**@preserve/*@cc_on!@*/false) { IE fixup... }
虽然这会在压缩后产生所需的结果,但它不是源代码形式的功能条件注释。 但出于调试原因,我需要我的JavaScript文件才能同时处理压缩和未压缩。
我是否有希望在不修改压缩的JS文件的情况下使其正常工作?
作为参考,这是我原始形式的完整功能:
function findEvent(e)
{
e = e || event; // IE
if (!e.target && e.srcElement) // IE
e.target = e.srcElement;
if (isSet(e.button))
{
// Every browser uses different values for the mouse buttons. Correct them here.
// DOM says: 0 = left, 1 = middle, 2 = right (multiple buttons not supported)
// Opera 7 and older and Konqueror are not specifically handled here.
// See http://de.selfhtml.org/javascript/objekte/event.htm#button
if (/*@cc_on!@*/false) // IE - http://dean.edwards.name/weblog/2007/03/sniff/ - comment removed by Google Closure Compiler
{
if (e.button & 1)
e.mouseButton = 0;
else if (e.button & 2)
e.mouseButton = 2;
else if (e.button & 4)
e.mouseButton = 1;
}
else
e.mouseButton = e.button;
}
return e;
}
在脚本的开头有两行似乎已经检测到浏览器是IE。 为什么不将它用作if语句的基础:
var IE = (!e.target && e.srcElement);
if (IE) {
!e.target && e.srcElement
}
// ...
if (IE) {
if (e.button & 1)
e.mouseButton = 0;
// ...
你可以有一个单独的脚本来设置一个全局(比如jQuery“.browser()”的东西),并让条件注释包含它:
<!--[if IE]>
<script>
window.isIe = true;
</script>
<[endif]-->
事实上,你可以让这更加漂亮:
<!--[if = IE 6]>
<script>
window.isIe = { version: 6 };
</script>
<[endif]-->
<!--[if = IE 7]>
<script>
window.isIe = { version: 7 };
</script>
<[endif]-->
<!--[if = IE 8]>
<script>
window.isIe = { version: 8 };
</script>
<[endif]-->
然后在压缩的“真实”脚本中,您可以这样做:
if (isIe) { /* IE stuff */ }
和
if (isIe && isIe.version === 6) { /* IE6 stuff */ }
你可以这样做:
var is_ie = eval(“/ * @ cc_on!@ * / false”);
http://code.google.com/p/closure-compiler/wiki/UsingConditionalCommentWithClosureCompiler
if (goog.userAgent.IE) {
// run away
};
/**
* Condition will be true for IE < 9.0
* @see goog.string.compareVersions
*/
if (goog.userAgent.IE && goog.userAgent.compare(9, goog.userAgent.VERSION) == 1) {
// run away even faster
}
/**
* Condition will be true for IE >= 10.0
*/
if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher(10)) {
// ...
}
只是偶然发现了这一点,所以这是2013年的最新答案:
if (goog.userAgent.IE) { ... }
如果您需要特定于版本:
if (goog.userAgent.IE && goog.userAgent.VERSION != '10.0') { ... }
好的,经过一番搜索,现在我对这个解决方案很满意:
function ieVersion()
{
var style = document.documentElement.style;
if (style.scrollbar3dLightColor != undefined)
{
if (style.opacity != undefined)
return 9;
else if (style.msBlockProgression != undefined)
return 8;
else if (style.msInterpolationMode != undefined)
return 7;
else if (style.textOverflow != undefined)
return 6;
else
return 5.5;
}
return 0;
}
发现于http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx#4
这是我在路上找到的其他方法的列表:
http://dean.edwards.name/weblog/2007/03/sniff/ - 不适用于Closure Compiler
http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html - 这就是带有“\\ v”的那个
http://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/ - 与上述类似,也适用于其他浏览器
如何使用Google Closure Compiler在JavaScript中检测Internet Explorer? - John在此页面上的回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.