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

前端 未结 4 710
萌比男神i
萌比男神i 2021-01-01 20:48

I am working on a react application and customers want it to show a special message if user\'s old browser does not support (e.g. IE 9).

So

相关标签:
4条回答
  • 2021-01-01 21:14

    I found a lot of JS scripts to detect user's browser name and version, so I had to mix them to get exactly what I want

    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>
    

    This script allows users to proceed if their browser is chrome >= 48 or ie >= 10 or any version of edge. Otherwise it shows a special message asking user to update or change his browser.

    You can also customize this script to your needs, modifying isSupported() function.

    0 讨论(0)
  • 2021-01-01 21:27

    I think the best and most user-friendly alternative is to redirect the user to an exclusive ie.html page, where you can show instructions about how to download other browsers and just care about all IE stuff in that page only.

    This way you don't need to do anything with React, just add the lines below to your index.html:

    <script type="application/javascript">
        if (/MSIE|Trident/.test(window.navigator.userAgent)) window.location.href = '/ie.html';
    </script>
    
    0 讨论(0)
  • 2021-01-01 21:32

    Well what you can do is, you have to check the browser version, like in my case I was dealing with the Internet Explorer Version older than 11. So detect the browser. There is no need to add an extra package, just few lines of code and then make two ReactDOm.renders, one for Success/Supported and the other for Unsupported Version.

    Here is how I did it, maybe that will help you:

    // 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')
      );
    }
    
    0 讨论(0)
  • 2021-01-01 21:40

    You have detect browser package on npm that may help you

    0 讨论(0)
提交回复
热议问题