Skip to content

Instantly share code, notes, and snippets.

@Craga89
Created May 29, 2012 16:39
Show Gist options
  • Save Craga89/2829457 to your computer and use it in GitHub Desktop.
Save Craga89/2829457 to your computer and use it in GitHub Desktop.
JavaScript iOS version detection
/*
* Outputs a float representing the iOS version if user is using an iOS browser i.e. iPhone, iPad
* Possible values include:
* 3 - v3.0
* 4.0 - v4.0
* 4.14 - v4.1.4
* false - Not iOS
*/
var iOS = parseFloat(
('' + (/CPU.*OS ([0-9_]{1,5})|(CPU like).*AppleWebKit.*Mobile/i.exec(navigator.userAgent) || [0,''])[1])
.replace('undefined', '3_2').replace('_', '.').replace('_', '')
) || false;
@kthy
Copy link

kthy commented Oct 17, 2017

.replace('_', '.').replace('_', '') is weird - there would be no underscores to replace for the second call, as the first one makes them into periods, right? Is this a typo and something else should be replaced?

@phil-martin
Copy link

Unfortunately the javascript string replace only replaces the first occurrence, not all occurrences. The code above replaces the first underscores with a period, and then the second underscore with nothing.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

@andrewhosgood
Copy link

.replace( /_/g, '' )?

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