Check if user is using IE with jQuery

2018-12-31 14:51发布

I am calling a function like the one below by click on divs with a certain class.

Is there a way I can check when starting the function if a user is using Internet Explorer and abort / cancel it if they are using other browsers so that it only runs for IE users ? The users here would all be on IE8 or higher versions so I would not need to cover IE7 and lower versions.

If I could tell which browser they are using that would be great but is not required.

Example function:

$('.myClass').on('click', function(event)
{
    // my function
});

27条回答
看淡一切
2楼-- · 2018-12-31 15:25

You can use the navigator object to detect user-navigator, you don't need jquery for it

<script type="text/javascript">

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1 ){ 

 // do stuff with ie-users
}

</script>

http://www.javascriptkit.com/javatutors/navigator.shtml

查看更多
琉璃瓶的回忆
3楼-- · 2018-12-31 15:25

Method 01:
$.browser was deprecated in jQuery version 1.3 and removed in 1.9

if ( $.browser.msie) {
  alert( "Hello! This is IE." );
}

Method 02:
Using Conditional Comments

<!--[if gte IE 8]>
<p>You're using a recent version of Internet Explorer.</p>
<![endif]-->

<!--[if lt IE 7]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<![endif]-->

<![if !IE]>
<p>You're not using Internet Explorer.</p>
<![endif]>

Method 03:

 /**
 * Returns the version of Internet Explorer or a -1
 * (indicating the use of another browser).
 */
function getInternetExplorerVersion()
{
    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat( RegExp.$1 );
    }

    return rv;
}

function checkVersion()
{
    var msg = "You're not using Internet Explorer.";
    var ver = getInternetExplorerVersion();

    if ( ver > -1 )
    {
        if ( ver >= 8.0 ) 
            msg = "You're using a recent copy of Internet Explorer."
        else
            msg = "You should upgrade your copy of Internet Explorer.";
    }

    alert( msg );
}

Method 04:
Use JavaScript/Manual Detection

/*
     Internet Explorer sniffer code to add class to body tag for IE version.
     Can be removed if your using something like Modernizr.
 */
 var ie = (function ()
 {

     var undef,
     v = 3,
         div = document.createElement('div'),
         all = div.getElementsByTagName('i');

     while (
     div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i>< ![endif]-->',
     all[0]);

     //append class to body for use with browser support
     if (v > 4)
     {
         $('body').addClass('ie' + v);
     }

 }());

Reference Link

查看更多
人气声优
4楼-- · 2018-12-31 15:27

I've placed this code in the document ready function and it only triggers in internet explorer. Tested in Internet Explorer 11.

var ua = window.navigator.userAgent;
ms_ie = /MSIE|Trident/.test(ua);
if ( ms_ie ) {
    //Do internet explorer exclusive behaviour here
}
查看更多
还给你的自由
5楼-- · 2018-12-31 15:28

If you don't want to use the useragent, you could also just do this for checking if the browser is IE. The commented code actually runs in IE browsers and turns the "false" to "true".

var isIE = /*@cc_on!@*/false;
if(isIE){
    //The browser is IE.
}else{
    //The browser is NOT IE.
}   
查看更多
宁负流年不负卿
6楼-- · 2018-12-31 15:28

i've used this

function notIE(){
    var ua = window.navigator.userAgent;
    if (ua.indexOf('Edge/') > 0 || 
        ua.indexOf('Trident/') > 0 || 
        ua.indexOf('MSIE ') > 0){
       return false;
    }else{
        return true;                
    }
}
查看更多
弹指情弦暗扣
7楼-- · 2018-12-31 15:28

Yet another simple (yet human readable) function to detect if the browser is IE or not (ignoring Edge, which isn't bad at all):

function isIE() {
  var ua = window.navigator.userAgent;
  var msie = ua.indexOf('MSIE '); // IE 10 or older
  var trident = ua.indexOf('Trident/'); //IE 11

  return (msie > 0 || trident > 0);
}
查看更多
登录 后发表回答