【问题标题】:Can I render warning message if user's browser is not supported?如果不支持用户的浏览器,我可以呈现警告消息吗?
【发布时间】:2019-03-15 03:09:19
【问题描述】:

我正在开发一个 react 应用程序,如果用户的旧浏览器不支持 (例如 IE 9),客户​​希望它显示一条特殊消息。

这么久以来,我尝试使用react-device-detect 包检测一些“流行”的旧浏览器。

src/index.js

import { browserName, browserVersion } from "react-device-detect";

const render = Component => {
  if (browserName === "IE" && browserVersion < 10) {
    ReactDOM.render(<UnsupportedBrowser />, document.getElementById("root"));
  } else {
    ReactDOM.render(
      <AppContainer>
        <Component store={store} history={history} />
      </AppContainer>,
      document.getElementById("root")
    );
  }
};

并放置条件 cmets:

public/index.html

<!--[if lte IE 9]>
  Please upgrade your browser
<![endif]-->

但我怀疑有更好的方法,但我在网上找不到。

【问题讨论】:

标签: reactjs google-chrome internet-explorer cross-browser


【解决方案1】:

你在 npm 上有 detect browser 包,可能会对你有所帮助

【讨论】:

    【解决方案2】:

    我发现了很多 JS 脚本来检测用户的浏览器名称和版本,所以我不得不混合它们以获得我想要的结果

    public/index.html

    <script type="text/javascript">
        function get_browser() {
          var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
          if (/trident/i.test(M[1])) {
            tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
            return { name: 'IE', version: (tem[1] || '') };
          }
          if (M[1] === 'Chrome') {
            tem = ua.match(/\bOPR\/(\d+)/)
            if (tem != null) { return { name: 'Opera', version: tem[1] }; }
          }
          if (window.navigator.userAgent.indexOf("Edge") > -1) {
            tem = ua.match(/\Edge\/(\d+)/)
            if (tem != null) { return { name: 'Edge', version: tem[1] }; }      
          }
          M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
          if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); }
          return {
            name: M[0],
            version: +M[1]
          };
        }
    
        var browser = get_browser()
        var isSupported = isSupported(browser);
    
        function isSupported(browser) {
          var supported = false;
          if (browser.name === "Chrome" && browser.version >= 48) {
            supported = true;
          } else if ((browser.name === "MSIE" || browser.name === "IE") && browser.version >= 10) {
            supported = true;
          } else if (browser.name === "Edge") {
            supported = true;
          }
          return supported;
        }
    
        if (!isSupported) {
          document.write(<h1>My message</h1>)
        }
      </script>
    

    如果用户的浏览器是 chrome >= 48 或 ie >= 10 或任何版本的 edge,此脚本允许用户继续。否则,它会显示一条特殊消息,要求用户更新或更改他的浏览器。

    您也可以根据需要自定义此脚本,修改 isSupported() 函数。

    【讨论】:

    • 它显示一条错误消息“我的消息”,但 2 分钟后它会加载应用程序..
    • 这是黄金。谢谢。
    • @SaravanapriyanS 我认为您应该为您的特定网站自定义 isSupported 函数。
    • 这不想在 IE11 中为我工作。剧本一直在悄悄地失败。我终于找到了document.write(&lt;h1&gt;My message&lt;/h1&gt;) 部分——IE11 将在这一行被炸毁,除非你将 HTML 用引号括起来。否则就像一个魅力 - 非常感谢!
    【解决方案3】:

    你可以做的是,你必须检查浏览器版本,就像在我的情况下,我处理的是早于 11 的 Internet Explorer 版本。所以检测浏览器。无需添加额外的包,只需几行代码,然后制作两个ReactDOm.renders,一个用于Success/Supported,另一个用于Unsupported Version。

    这是我的做法,也许对你有帮助:

    // get the Version of Browser, older than '11'
    const getIEVersion = () => {
      const ua = window.navigator.userAgent;
      const msie = ua.indexOf('MSIE ');
      if (msie > 0) {
        // IE 10 or older => return version number
        return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
      }
      const trident = ua.indexOf('Trident/');
      if (trident > 0) {
        // IE 11 => return version number
        const rv = ua.indexOf('rv:');
        return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
      }
      const edge = ua.indexOf('Edge/');
      if (edge > 0) {
        // Edge (IE 12+) => return version number
        return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
      }
      // other browser
      return false;
    };
    
    // Method for Rendering of Unsupported Version
    const UnsupportedBrowserPage = () => (
      <div>
        <p>Test Page For Versions lower then 11 on IE</p>
      </div>
    );
    
    
    const iEVersion = getIEVersion();
    
    if (iEVersion && iEVersion < 11) {
    
    ReactDOM.render(<UnsupportedBrowserPage />, document.getElementById('root'));
    
    } else {
      ReactDOM.render(
        <SupportedApp />, document.getElementById('root')
      );
    }
    

    【讨论】:

      【解决方案4】:

      我认为最好和最用户友好的替代方法是将用户重定向到专用的ie.html 页面,您可以在其中显示有关如何下载其他浏览器的说明,并且只关心该页面中的所有 IE 内容。

      这样你不需要对 React 做任何事情,只需将下面的行添加到你的 index.html:

      <script type="application/javascript">
          if (/MSIE|Trident/.test(window.navigator.userAgent)) window.location.href = '/ie.html';
      </script>
      

      【讨论】:

      • 很好的解决方案!
      猜你喜欢
      • 2020-07-11
      • 2018-02-17
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      相关资源
      最近更新 更多