简体   繁体   English

如何使用Google Closure Compiler在JavaScript中检测Internet Explorer?

[英]How to detect Internet Explorer in JavaScript with Google Closure Compiler?

I have a JavaScript function that handles mouse button events. 我有一个处理鼠标按钮事件的JavaScript函数。 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. 遗憾的是,Internet Explorer对event.button使用的值与其他浏览器不同。 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. 我使用依赖于条件编译的JavaScript hack来做到这一点。 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. 我认为这是一种非常安全的方法,因为它基于JavaScript解析器功能,不能伪造,也不太可能被其他浏览器模仿。

Now I'm using the Google Closure Compiler to pack my JavaScript files. 现在我正在使用Google Closure Compiler打包我的JavaScript文件。 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. 此外,我不喜欢它,因为微软可能最终修复\\ v错误,然后检测失败。

I could just read something like navigator.appName or what it's called, but this is way too easy to fake. 我可以读取类似navigator.appName或者它所调用的内容,但这很容易伪造。 And if somebody modifies their browser identification, they're unlikely to implement the other event.button behaviour... 如果某人修改了他们的浏览器标识,他们就不太可能实现其他event.button行为......

Closure compiler allows to preserve certain comments. Closure编译器允许保留某些注释。 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. 但出于调试原因,我需要我的JavaScript文件才能同时处理压缩和未压缩。

Is there any hope for me to get this working without modifying the compressed JS file by hand? 我是否有希望在不修改压缩的JS文件的情况下使其正常工作?

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;
}

You have two lines at the beginning of your script which seem to already detect if the browser is IE. 在脚本的开头有两行似乎已经检测到浏览器是IE。 Why not use that as a base for your if statement : 为什么不将它用作if语句的基础:

var IE = (!e.target && e.srcElement);
if (IE)  {
    !e.target && e.srcElement
}
// ...
if (IE) {
    if (e.button & 1)
        e.mouseButton = 0;
    // ...

You could have a separate script that sets up a global (like the jQuery ".browser()" thing), and have it be included by conditional comment: 你可以有一个单独的脚本来设置一个全局(比如jQuery“.browser()”的东西),并让条件注释包含它:

<!--[if IE]>
<script>
  window.isIe = true;
</script>
<[endif]-->

In fact you could make this even fancier: 事实上,你可以让这更加漂亮:

<!--[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]-->

Then in your compressed "real" script you can just do: 然后在压缩的“真实”脚本中,您可以这样做:

if (isIe) { /* IE stuff */ }

and

if (isIe && isIe.version === 6) { /* IE6 stuff */ }

You can do this: 你可以这样做:

var is_ie = eval("/*@cc_on!@*/false"); var is_ie = eval(“/ * @ cc_on!@ * / false”);

http://code.google.com/p/closure-compiler/wiki/UsingConditionalCommentWithClosureCompiler 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)) {
    // ...
}

source : http://www.closurecheatsheet.com/other#goog-useragent 来源: http//www.closurecheatsheet.com/other#goog-useragent

Just stumbled on this, so here's an updated answer for 2013: 只是偶然发现了这一点,所以这是2013年的最新答案:

if (goog.userAgent.IE) { ... }

And if you need to be version-specific: 如果您需要特定于版本:

if (goog.userAgent.IE && goog.userAgent.VERSION != '10.0') { ... }

Docs 文件

Okay, after some more searching, for now I'm happy with this solution: 好的,经过一番搜索,现在我对这个解决方案很满意:

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;
}

Found on http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx#4 发现于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 Compiler 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 – That's the one with "\\v" 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/ – Similar to above, also for other browsers http://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/ - 与上述类似,也适用于其他浏览器
How to detect Internet Explorer in JavaScript with Google Closure Compiler? 如何使用Google Closure Compiler在JavaScript中检测Internet Explorer? – Answer by John on this page - John在此页面上的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM