title: Javascript - detecting browser version
body: <p>I know this is not the most reliable way, but I don't really have many other choices.  I essentially need to detect what browser and version visits my website.  If it is a certain browser/version, it will display a message.  This is for the TLS issue, whereby certain browser versions will not display a https page.</p>
  
  <p>Anyways, at the moment I have something like the following</p>
  
  <pre><code>function getBrowserInfo()
  {
      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 'IE '+(tem[1] || '');
      }
      if(M[1]=== 'Chrome')
      {
          tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
          if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
      }
      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 M.join(' ');
  }
  
  var browserInfo = getBrowserInfo();
  browserInfo = browserInfo.split(" ");
  
  $(function () {
      alert(browserInfo);
      if(browserInfo[0] == 'Firefox' &amp;&amp; browserInfo[1] &lt; 27) {
          alert("Firefox");
      }
      if(browserInfo[0] == 'Chrome' &amp;&amp; browserInfo[1] &lt; 22) {
          alert("Chrome");
      }
      if(browserInfo[0] == 'IE' &amp;&amp; browserInfo[1] &lt; 11) {
          alert("IE");
      }
      if(browserInfo[0] == 'Safari' &amp;&amp; browserInfo[1] &lt; 11) {
          alert("Safari");
      }
      if(browserInfo[0] == 'Opera' &amp;&amp; browserInfo[1] &lt; 14) {
          alert("Safari");
      }
  });
  </code></pre>
  
  <p>One thing that concerns me is the splitting of the string, incase a browser returns a different format.  Additionally, I think all of these if statements are redundant, but I cant think of a more efficient way to do this.</p>
  
  <p>Essentially, I am looking for advice as to the best way to detect a browser, and its version, so I can display a custom message for that browser.</p>
  
  <p>Thanks</p>