如何使用 Google Closure 编译器检测 JavaScript 中的 Internet Explorer?
我有一个处理鼠标按钮事件的 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 行为...
闭包编译器允许保留某些注释。我尝试了这个:
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;
}
I have a JavaScript function that handles mouse button events. It must be able to distinguish between left and right mouse buttons. Sadly, Internet Explorer uses different values for event.button than all other browsers do. I know how to interpret them, but I need to know which path to go.
I did that with a JavaScript hack that relies on conditional compilation. It's like this:
if (/*@cc_on!@*/false) { IE fixup... }
I consider this quite a safe method because it is based on the JavaScript parser capabilities that cannot be faked and are unlikely to be imitated by other browsers.
Now I'm using the Google Closure Compiler to pack my JavaScript files. I found that it removes conditional compilation comments just like any other comment, too. So I tried different hacks. One of them it this:
if ("\v" == "v") { IE fixup... }
Unfortunately, the closure compiler quite clever and finds out that the condition can never be true and removes that code. Also, I don't like it because Microsoft may eventually fix that \v bug and then the detection fails.
I could just read something like navigator.appName or what it's called, but this is way too easy to fake. And if somebody modifies their browser identification, they're unlikely to implement the other event.button behaviour...
Closure compiler allows to preserve certain comments. I tried this:
if (/**@preserve/*@cc_on!@*/false) { IE fixup... }
While this produces the desired result after compression, it is not a functional conditional comment in its source form. But for debugging reasons, I need my JavaScript file to work both compressed and uncompressed.
Is there any hope for me to get this working without modifying the compressed JS file by hand?
For the reference, here's my complete function in its original form:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的脚本开头有两行,似乎已经检测浏览器是否为 IE。为什么不使用它作为 if 语句的基础:
You have two lines at the beginning of your script which seem to already detect if the browser is IE. Why not use that as a base for your if statement :
你可以有一个单独的脚本来设置一个全局的(就像jQuery的“.browser()”东西),并把它包含在条件注释中:
事实上,你可以让它变得更奇特:
然后在压缩的“真实”脚本中,您可以执行以下操作
:
You could have a separate script that sets up a global (like the jQuery ".browser()" thing), and have it be included by conditional comment:
In fact you could make this even fancier:
Then in your compressed "real" script you can just do:
and
你可以这样做:
var is_ie = eval("/*@cc_on!@*/false");
http://code.google.com/p/closure-compiler/wiki/UsingConditionalCommentWithClosureCompiler
You can do this:
var is_ie = eval("/*@cc_on!@*/false");
http://code.google.com/p/closure-compiler/wiki/UsingConditionalCommentWithClosureCompiler
来源:http://www.closurecheatsheet.com/other#goog-useragent
source : http://www.closurecheatsheet.com/other#goog-useragent
好的,经过更多搜索后,现在我对这个解决方案感到满意:
在 http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx#4
以下是其他方法的列表我在路上发现:
http://dean.edwards.name/weblog/2007/03/ sniff/– 不适用于闭包编译器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 编译器在 JavaScript 中检测 Internet Explorer? – John 在本页上的回答
Okay, after some more searching, for now I'm happy with this solution:
Found on http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx#4
Here's a list of other methods I found on the way:
http://dean.edwards.name/weblog/2007/03/sniff/– Doesn't work with Closure Compilerhttp://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html – That's the one with "\v"
http://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/ – Similar to above, also for other browsers
How to detect Internet Explorer in JavaScript with Google Closure Compiler? – Answer by John on this page
只是偶然发现了这一点,所以这是 2013 年的更新答案:
如果您需要特定于版本:
文档
Just stumbled on this, so here's an updated answer for 2013:
And if you need to be version-specific:
Docs