title: How to detect Internet Explorer in JavaScript with Google Closure Compiler?
body: <p>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.</p>
  
  <p>I did that with a JavaScript hack that relies on conditional compilation. It's like this:</p>
  
  <pre><code>if (/*@cc_on!@*/false) { IE fixup... }
  </code></pre>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>if ("\v" == "v") { IE fixup... }
  </code></pre>
  
  <p>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.</p>
  
  <p>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...</p>
  
  <p>Closure compiler allows to preserve certain comments. I tried this:</p>
  
  <pre><code>if (/**@preserve/*@cc_on!@*/false) { IE fixup... }
  </code></pre>
  
  <p>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.</p>
  
  <p>Is there any hope for me to get this working without modifying the compressed JS file by hand?</p>
  
  <p>For the reference, here's my complete function in its original form:</p>
  
  <pre><code>function findEvent(e)
  {
      e = e || event;   // IE
      if (!e.target &amp;&amp; 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 &amp; 1)
                  e.mouseButton = 0;
              else if (e.button &amp; 2)
                  e.mouseButton = 2;
              else if (e.button &amp; 4)
                  e.mouseButton = 1;
          }
          else
              e.mouseButton = e.button;
      }
      return e;
  }
  </code></pre>