【问题标题】:How to stop a JavaScript function from running in IE but not in other browsers? [duplicate]如何阻止 JavaScript 函数在 IE 中运行而不在其他浏览器中运行? [复制]
【发布时间】:2014-08-06 23:30:05
【问题描述】:

所以我有一个 javascript 函数,它在单击站点中的链接时运行。

$('.mainNavWrap .preloadNav a, .bannerNav li a').click(function(event){
    event.preventDefault();
    linkLocation = this.href;
    $('.preloader2').fadeIn(100, redirectPage);
});
function redirectPage() {
    setTimeout(function(){
        window.location = linkLocation;
    }, 1000);

}

该函数在页面重定向之前调用预加载器。如何禁用 ie8 的整个功能?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

方法一

if ( $.browser.msie ) $('.preloader2').fadeIn(100, redirectPage);

注意:除非包含 jQuery Migrate 插件,否则这在 jQuery 1.9 或更高版本中不起作用。

方法二

(Detect IE version (prior to v9) in JavaScript)

在html中添加:

<!doctype html>
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->

然后在你的脚本中使用它:

if ($('html').is('.ie8')) $('.preloader2').fadeIn(100, redirectPage);

【讨论】:

  • $.browser 在 jquery 1.9 中被删除
【解决方案2】:

如果用户代理匹配 IE8,则返回 false。通常不建议使用浏览器嗅探,但这几乎是唯一的方法,而不需要访问更多的浏览器。

Here's a list of UA strings

  function redirectPage() {
        var ie = (navigator.userAgent.match(/(MSIE)/g) ? true : false );
       if (ie == true) {
    return false; // or browser-specific code
    } else {
        setTimeout(function(){
            window.location = linkLocation;
        }, 1000);
    }
    }

这是从手机上写的,所以请原谅格式。

【讨论】:

    【解决方案3】:

    也许你可以试试这个:

            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");
            if (msie > 0)
            {
               /* CODING SPECIFIC TO IE BROWSER */
               if (parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))) == 8)
               {   /* CODING SPECIFIC TO IE8*/}
            }   
            else                 
            {
                 /* CODING FOR OTHER BROWSERS*/
             }
        }
    

    【讨论】:

    • 这很好用。非常感谢:D
    【解决方案4】:

    检查 SVG 支持,因为 ie8 不支持 ie8 以下是代码:-

     if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) {
     if (!document.createElement('SVG').getAttributeNS) {
         if (document.createElement('DATA').getAttributeNS) {
           //the browser is defently IE8
             return;
          }
       }
    }
    else
    {
    //call your function
    }
    

    或者另一种方式是:- 检查ie8的函数

    function isIE () 
    {
      var myNav = navigator.userAgent.toLowerCase();
      return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
     }
    
     if (isIE () == 8) {
      // IE8 code
     } else {
       // Other versions IE or not IE
      }
    

    【讨论】:

    • 非 IE8 浏览器也不支持 SVG,所以你会用这种方法访问太多浏览器
    • 那么你可以使用第二种方法
    【解决方案5】:

    jQuery 具有查看浏览器是否为任何形式的 MSIE 的功能

    if (!$.browser.msie) {
       // jquery function
    }
    

    【讨论】:

    • $.browser 在 jquery 1.9 中被删除
    猜你喜欢
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多