簡體   English   中英

如何使用Google Closure Compiler在JavaScript中檢測Internet Explorer?

[英]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)) {
    // ...
}

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

只是偶然發現了這一點,所以這是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM