【问题标题】:Script for show message only in IE browser [duplicate]仅在 IE 浏览器中显示消息的脚本[重复]
【发布时间】:2019-04-24 09:56:33
【问题描述】:

您知道仅适用于 Internet Explorer 用户的通知或警告脚本吗?

我只需要在此特定浏览器中为用户显示警告。

拜托,你能帮帮我吗?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    前段时间我不得不解决这个问题。由于放弃了对条件 cmets 的支持,我最终使用了 javascript:https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/hh801214(v=vs.85)

    我的解决方案最终看起来像这样:

    <style>
        #ie-banner {
            display: none;
            /* other styling */
        }
    </style>
    
    <div id="ie-banner">
      <div id="ie-message">
        <h5>INCOMPATIBLE BROWSER</h5>
      </div>
    </div>
    
    <script>
        function isOldIE(userAgent) {
          var msie = userAgent.indexOf('MSIE');
          if (msie > 0) {
            // IE 10 or older
            return true;
          }
          // other browser, IE 11, or Edge
          return false;
        }
        if (isOldIE(navigator.userAgent)) {
          var ieWarning = document.getElementById('ie-banner');
          ieWarning.setAttribute('style', 'display: block;');
          // drop off my react app below
          // var root = document.getElementById('root');
          // document.body.removeChild(root);
        }
    </script>
    

    请注意,我删除了这样的子元素并使用较旧的 DOM api,因为更多标准方法根本无法在 IE 上运行...大惊喜。

    如果您只关心 IE9 及以下版本,那么我可能只会使用条件 cmets。直接来自上面的链接:

    <html>
      <!--[if IE]>
        This content is ignored in IE10 and other browsers.
        In older versions of IE it renders as part of the page.
      <![endif]-->
    </html>
    

    【讨论】:

    • 但我想在资源管理器中显示一条消息。没有版本。在我的情况下,我不明白你的代码
    猜你喜欢
    • 1970-01-01
    • 2011-01-11
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多