Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active December 26, 2015 06:29
Show Gist options
  • Save WebReflection/7107617 to your computer and use it in GitHub Desktop.
Save WebReflection/7107617 to your computer and use it in GitHub Desktop.
navigator.version (major, minor, revision) where exposed and possibl updated to be as cross browser as possible
/** Andrea Giammarchi
* WTFPL - http://www.wtfpl.net/
*/
'version' in navigator || (
navigator.version =
/(?:(?:OS(?: X)?|Android|Windows(?: NT)) |(?:IEMobile|Version|webOS|Nokia\w+)\/)(\d+)[_.](\d+)(?:[_.](\d+(?:\.\d+)?))?/
.test(navigator.userAgent) ?
{
major: RegExp.$1,
minor: RegExp.$2 || '0',
// let me introduce you BB10 versioning ...
revision: (RegExp.$3 || '0').replace(/^0\./, ''),
// compare with coercion
// if (os.version == 7) {}
// NOTE: the == instead of ===
// works with other operators too
// such os.version < 7 etc
valueOf: function () {
return this.major;
}
} :
{} // unknown (some browser deos not expose the OS)
);
@WebReflection
Copy link
Author

Tested With

  • IE 10 Mobile and Desktop
  • Chrome on Mac OS X
  • Safari on Mac OS X
  • Android 2.3.X, 4.3
  • Blackberry Z10
  • Nokia Asha 510
  • webOS 2
  • iOS 5.1, 6, 7

On Windows XP and Windows 7

  • Firefox 24
  • Chrome 30
  • Opera 12

Please visit this test page and tell me if the result is correct (first line) and your OS is not mentioned in above list, thank you.

Firefox does not show the OS within its user agent. If there's an undefined value, most likely that's Firefox Browser/OS

@dfkaye
Copy link

dfkaye commented Oct 23, 2013

All of these 3
these work for me
along with IE
on Windows XP
and Win7:

  • Firefox 24
  • Chrome 30
  • Opera 12

@WebReflection
Copy link
Author

updated, thanks!

@swernerx
Copy link

Returns the OS version (10.9.0 under Chrome, Mavericks) for me... is this what it should? Wondering why it is assigned to "navigator.version" then. Wouldn't it be better called "navigator.osVersion"?

@WebReflection
Copy link
Author

that's about the WTFPL :) Thanks.
P.S. yes, 10.9 makes sense, I haven't updated to that and I am on 10.8.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment