3

我有一个处理鼠标按钮事件的JavaScript函数。它必须能够区分鼠标左键和右键。可悲的是,Internet Explorer对event.button使用的值不同于其他所有浏览器。我知道如何解释它们,但我需要知道要走哪条路。如何使用Google Closure Compiler在JavaScript中检测Internet Explorer?

我做了一个JavaScript的黑客工具,依赖于条件编译。它是这样的:

if (/*@[email protected]*/false) { IE fixup... } 

我认为这是一个相当安全的方法,因为它是基于无法伪造并不太可能被其他浏览器模仿的JavaScript分析器功能。

现在我使用的是谷歌关闭编译收拾我的JavaScript文件。我发现它也像其他评论一样删除了条件编译注释。所以我尝试了不同的黑客。其中之一是这样的:

if ("\v" == "v") { IE fixup... } 

不幸的是,闭包编译器很聪明,发现条件永远不会是真的,并删除该代码。另外,我不喜欢它,因为微软可能最终修复这个\ v bug,然后检测失败。

我可以读的东西像navigator.appName或者它叫什么,但是这是太容易伪造。如果有人修改了自己的浏览器识别,他们是不太可能实现的其他event.button行为......

Closure编译器允许保留一定意见。我尝试这样做:

if (/**@preserve/*@[email protected]*/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 (/*@[email protected]*/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; 
} 

回答

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/ - 不与关闭编译工作
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/ - 上面类似,也为其他浏览器
How to detect Internet Explorer in JavaScript with Google Closure Compiler? - 约翰回答这个页面

2

你必须在脚本的开头两行,这似乎已经发现,如果浏览器是IE。为什么不使用,作为一个基础的if语句:

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

是event.srcElement存在一个肯定的条件来检测IE?我完全没有这方面的细节。 srcElement/target代码适用于单行(检测功能,而不是浏览器),但可能会在其他任何地方失败。不幸的是,event.button的值不能被特征检测解释。 – ygoe 2011-02-28 07:11:21

2

你可以有一个独立的脚本,设置了一个全球性的(如jQuery的”。浏览器()”的东西),并将它包含在由条件注释:

<!--[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 */ } 
0

只是无意中发现了这个,所以这里是2013年更新的答案:

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

如果你需要特定版本:

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

Docs

+0

我不使用的Closure *库*但只有* *编译器。所以我需要编译时支持,而不是运行时。实际上,我的Js库在第一次看来似乎是封闭库看起来很简单的一个版本。 – ygoe 2013-11-26 09:26:53

+0

啊,好吧。那么,我会把这里留给后人,以防有人想知道如何与图书馆合作。 – 2013-11-26 14:14:32

1
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)) { 
    // ... 
} 

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

+0

澄清我的问题,因为我得到多个不相关答案我​​的老问题。我正在谈论Closure * Compiler *,而不是同名的库(如果存在这样的事情,我从来不关心它,也不使用它)。 – ygoe 2013-12-03 17:27:35

相关问题