Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
@DaveFlash

This comment has been minimized.

Copy link

@DaveFlash DaveFlash commented Jul 27, 2016

very nice! was very easy to integrate in my code, as var iOS outputs a dotted number (4.0, 3.2 or in my case 9.3.3 or even 10*)
it's made my work much less stressful in checking which iOS version user use when visiting my site, as I only need to write an if-statment lik so: if(iOS < 9.3.3) { //do this } and then serve, or rather inject in the DOM, the correct css and js files based on that. I'm still working on figuring out how to take your parsefloat from javascript to php so I can excute my code on server before serving to client, but that should not be difficult to do. Seems like the parse function in php, "floatval" is not even remotely like parsefloat in javascript as to it's purpose and usefulness... so i'm guess I'm stuck on that. any help bringer ios-version-js to php is appreciated

@kthy

This comment has been minimized.

Copy link

@kthy 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

This comment has been minimized.

Copy link

@phil-martin phil-martin commented Nov 9, 2017

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

This comment has been minimized.

Copy link

@andrewhosgood andrewhosgood commented Nov 24, 2017

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

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