How to detect Internet Explorer in JavaScript using the Google Closure Compiler?

I have a JavaScript function that handles mouse button events. It should be able to distinguish between left and right mouse buttons. Unfortunately, Internet Explorer uses different values ​​for event.button than all other browsers. I know how to interpret them, but I need to know which way to go.

I did this using a JavaScript hack that relies on conditional compilation. This is something like this:

if (/*@cc_on!@*/false) { IE fixup... }

I consider this rather safe method because it is based on JavaScript parser features that cannot be faked and are unlikely to be simulated by other browsers.

Now I use the Google Closure compiler to pack JavaScript files. I found that it removes conditional compilation comments just like any other comments. So I tried different hacks. One of them:

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

Unfortunately, the closure compiler is pretty smart and discovers that the condition can never be true and removes this code. Also, I don’t like it because Microsoft may end up fixing this \ v error and then the detection will fail.

I could just read something like navigator.appName or what it called, but it's too easy to fake. And if someone changes the identity of their browser, they are unlikely to be able to implement other event.button behavior ...

The Closure compiler allows you to save specific comments. I tried this:

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;
}
+3
6

, , :

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
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/ - ,
Internet Explorer JavaScript Google Closure Compiler? -

0

script, , , , IE. if:

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

script, (, 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]-->

"" script :

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

if (isIe && isIe.version === 6) { /* IE6 stuff */ }
+2
source
+2
source
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

+1
source

Just stumbled upon this, so here's the updated answer for 2013:

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

And if you need to be version specific:

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

Docs

0
source

Source: https://habr.com/ru/post/1794939/


All Articles