How can I determine which version of IE a user is running in JavaScript?

后端 未结 12 1880
生来不讨喜
生来不讨喜 2021-02-04 04:48

In some existing code there is a test to see if the user is running IE, by checking if the object Browser.Engine.trident is defined and returns true.

But how can I deter

12条回答
  •  死守一世寂寞
    2021-02-04 05:23

    If you really want to be sure you are using IE and a specific version then you could obviously use IE's conditional tags to only run certain code within IE. It's not really that pretty but at least you can be sure that it is really IE and not some spoofed version.

    
    
    
    

    It's pretty self explanatory. In IE6 isIE is true and version is 6, In IE7 isIE is true and version is 7 otherwise isIE is false and version is -1

    Alternatively you could just roll your own solution using code plagarised from jQuery.

    var userAgent = navigator.userAgent.toLowerCase();
    var version = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
    var isIE = /msie/.test( userAgent ) && !/opera/.test( userAgent ),    
    

提交回复
热议问题