0

I was using a simple regex test like

is_safari_or_uiwebview = /(iPhone|iPod|iPad).*AppleWebKit/i.test(navigator.userAgent);

in my js. After compile it with Closure compiler (advanced optimization) it's not working anymore. It still give me the regex but navigator.useragent is missing.

Note, I'm not using closure library and do no intent to use at this point. How can I preserve this test?

js code

var browser_type = new RegExp('(iPhone|iPod|iPad).*AppleWebKit', 'i');
function is_safari_or_uiwebview() {
    return browser_type.test(navigator.userAgent);
}
window['is_safari_or_uiwebview'] = is_safari_or_uiwebview;

compiled code:

var l=/(iPhone|iPod|iPad).*AppleWebKit/i,l=/(iPhone|iPod|iPad).*AppleWebKit/i;function m(){return l.test(navigator.c)}window.is_safari_or_uiwebview=m;
Ruben Trancoso
  • 1,444
  • 5
  • 27
  • 55

1 Answers1

1

Simple solution: Use bracket notation, as explained in the documentation.

navigator['userAgent']

BUT, when I test your code in http://closure-compiler.appspot.com/home, it does not rename the property name with advanced compilation.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143