Skip to content

Instantly share code, notes, and snippets.

@jagdeepsingh
Created January 8, 2018 05:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagdeepsingh/9fabcc8b72b17e4d1f11f1fb1056c24f to your computer and use it in GitHub Desktop.
Save jagdeepsingh/9fabcc8b72b17e4d1f11f1fb1056c24f to your computer and use it in GitHub Desktop.
Browser name and version details in JavaScript
navigator.browserInfo = (function() {
  var output, tem, ua;
  ua = navigator.userAgent;
  tem = void 0;

  output = ua.match(/(android)\s([0-9\.]*)/i) || ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];

  if (/android/i.test(output[1])) {
    return {
      name: 'android',
      version: parseInt(output[2])
    };
  }

  if (/trident/i.test(output[1])) {
    tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
    return {
      name: 'ie',
      version: parseInt(tem[1]) || ''
    };
  }

  if (output[1] === 'Chrome') {
    tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
    if (tem !== null) {
      return {
        name: tem[1].replace('OPR', 'opera'),
        version: parseInt(tem[2])
      };
    }
  }

  output = output[2] ? [output[1], output[2]] : [navigator.appName, navigator.appVersion, '-?'];

  if ((tem = ua.match(/version\/(\d+)/i)) !== null) {
    output.splice(1, 1, tem[1]);
  }

  return {
    name: output[0].toLowerCase(),
    version: parseInt(output[1])
  };
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment