简体   繁体   中英

IE10 navigator.userAgent script doesn't work

I'm trying to detect if the browser is IE by not looking for IE browser-centric components but rather looking for navigator.userAgent containing either firefox or chrome. The following script works fine in FF and Chrome but seems to be ignored in IE10.

var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (!is_firefox && !is_chrome) {
    var message = "I'm an IE browser";
} else {
    var message = "I'm not an IE browser";
}

Though I'd rather join the discussion in the comment, I really can't. Just re-created my account. So to answer your question. There is likely a reason that it is failing, and goes to show why testing the userAgent is a bad idea.

If you have the Chrome Frame add-on installed, it will add "chromeframe" to the userAgent string, which will set is_chrome to true . If I remember correctly my IE 10 came with Chrome Frame. Write the userAgent string to console and see. In almost everyone's honest opinion, using the userAgent is just a bad idea, as is browser detection in general, and in the long run may not be the easiest. Once you understand that the userAgent string can be modified when add-ons are installed and user's can flat out change them, you realize the work cut out for you.

If you are trying to block or serve content depending on whether the user has IE, it is best to use feature detection or conditional comments. You can use this to set a js variable and then do whatever you need. Here is an old school way using conditional comments

<html>
   <head>
       <!--[if ie 7]>
       <script>var _ie=8;</script>
       <![endif]-->
       <!--[if ie 8]>
       <script>var _ie=8;</script>
       <![endif]-->
       <!--[if ie 9]>
       <script>var _ie=9;</script>
       <![endif]-->
       <script type="text/javascript">
           if(_ie && _ie<9){
              // do something here
              // will not run in any other browser, because _ie is undefined
           }
       </script>
   </head>

The drawback to this solution is that it can't be used in external files, so the conditional comments would be on the page and the check could be in either the page itself or the external file. There would be errors, because you check for the existence of the variable first

The simplest way in IE is to check for ActiveXObject , as it is proprietary, and is likely not going anywhere. So it is very likely to be future safe.

<script type="text/javascript">
    // test for ie
    var _ie=("ActiveXObject" in window);
</script>

Really, this is browser detection by feature detection, so it still goes with the norm, plus it's short and sweet.

You can also refer to this document

Instead of checking chrome and firefox, why not to check for IE itself.

var message;
if(navigator.userAgent.toLowerCase().indexOf('msie') > -1){
   message = "I'm an IE browser";
} else {
   message = "I'm not an IE browser";
}
alert(message);

Edit:====>

Try checking this:

navigator.userAgent.toLowerCase().indexOf('trident') > -1

Note: TRIDENT is the browser engine used by IE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM