Check if user is using IE

后端 未结 30 1599
心在旅途
心在旅途 2020-11-22 04:34

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

30条回答
  •  -上瘾入骨i
    2020-11-22 05:04

    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

    
    
    
    
    
    

    You're not using Internet Explorer.

    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 = '',
         all[0]);
    
         //append class to body for use with browser support
         if (v > 4)
         {
             $('body').addClass('ie' + v);
         }
    
     }());
    

    Reference Link

提交回复
热议问题