如果不支持用户的浏览器,我可以呈现警告消息吗?

Can I render warning message if user's browser is not supported?

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

很长一段时间以来,我一直试图使用 react-device-detect 包来检测一些 "popular" 旧浏览器。

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")
    );
  }
};

并添加条件注释:

public/index.html

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

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

您在 npm 上有 detect browser 个包可以帮助您

我发现了很多 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() 函数。

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

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

// 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')
  );
}

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

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

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