Version Number of Smartphone OS's [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to programmatically get iOS’s alphanumeric version string
Is there any future proof regex to get the version number of the following smartphone OS's from the user-agent?
Android
(I have found something like: /Androids+([d.]+)/ )
iOS
BlackBerry
Any advise would be much appreciated.
Clarification: Seems like the question is asking how to get the mobile device OS version in a web-app, probably using JS.
UPDATE:
After I got quite bashed to ask this question I want at least provide the solution I came up with:
supportedDevice: function() {
var supportedDevice = false;
var userAgent = window.navigator.userAgent;
// check for supported Android device
if ( /Android/.test(userAgent) ) {
var a_index = Number(userAgent.indexOf('Android')) + 8;
var a_version = +userAgent.substring(a_index, a_index+1);
if ( a_version >= 3 ) {
supportedDevice = true;
console.log('Android device supported!')
}
}
// check for iOS supported devices
else if ( /iPhone/.test(userAgent) ) {
var i_index = Number(userAgent.indexOf('iPhone OS')) + 10;
var i_version = +userAgent.substring(i_index, i_index+1);
if ( i_version >= 6 ) {
supportedDevice = true;
console.log('iPhone device supported!')
}
}
// check for iOS supported devices
else if ( /BlackBerry/.test(userAgent) ) {
var b_index = Number(userAgent.indexOf('Version/')) + 8;
var b_version = +userAgent.substring(b_index, b_index+1);
if ( b_version >= 6 ) {
supportedDevice = true;
console.log('BB device supported!')
}
}
return supportedDevice;
}

If you need to obtain the version number in a web app, your best bet would be to use the device User Agent and parse out the version number. A more robust method would be to look up the user agent in the WURFL database to obtain the device characteristics and the corresponding OS. The first method is simpler.
If you are using an app, most OS SDKs provide APIs to identify the version of the OS running on the device

Related

How can I check if the device, which is using my website, is a mobile user or not

I just want to check if the decive, which is using my website, is on a mobile or any other device. It's a quick question with a quick answer I hope.
If you are trying to see if the user's device is mobile, the MDN docs advises to look for the property maxTouchPoints in the navigator (or browser) object and see if the value is > 0.
In the past this used to be done with User Agent Sniffing (Read more here), i.e going through the user-agent header sent by the browser into the navigator.userAgent property to see if it contains certain keywords. This method however has limitations and may not always tell the right kind of device the user is on because many devices today support different browsers and features and vice versa.
Using User Agent Sniffing (Not recommended today, should be used only as a fallback)
var hasTouchScreen = false;
var UA = navigator.userAgent;
hasTouchScreen = (
/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
/\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
);
if (hasTouchScreen) {
// Device is likely mobile, so do stuff for mobile devices here.
}
Check using maxTouchPoints property and if > 0 in navigator object (MDN Docs Recommended)
var hasTouchScreen = false;
if ("maxTouchPoints" in navigator) {
hasTouchScreen = navigator.maxTouchPoints > 0;
}
if (hasTouchScreen) {
// Device is likely mobile, so do stuff for mobile devices here.
}
Be aware, that not all browsers may support that specification, so the navigator object may not have the property maxTouchPoints or some mobile devices may have large screens and some desktop devices may have small touch-screens or some people may use smart TVs and so on. So a better way to do this check would be to combine the snippet above with some fallbacks:
Better way to detect mobile devices using a combination of previous method and fallbacks (Most Robust Method, MDN Docs Recommended)
var hasTouchScreen = false;
if ("maxTouchPoints" in navigator) {
hasTouchScreen = navigator.maxTouchPoints > 0;
} else if ("msMaxTouchPoints" in navigator) {
hasTouchScreen = navigator.msMaxTouchPoints > 0;
} else {
var mQ = window.matchMedia && matchMedia("(pointer:coarse)");
if (mQ && mQ.media === "(pointer:coarse)") {
hasTouchScreen = !!mQ.matches;
} else if ('orientation' in window) {
hasTouchScreen = true; // deprecated, but good fallback
} else {
// Only as a last resort, fall back to user agent sniffing
var UA = navigator.userAgent;
hasTouchScreen = (
/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
/\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
);
}
}
if (hasTouchScreen)
// Do something here.
}
Read more about browser detection using the user agent and the recommended way for mobile device detection here (For the recommended method for mobile device detection, look under the "Mobile device detection" subheading).
Just use this condition in javascript:
if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)){
//do your stuff here
}

Detect MacOS, iOS, Windows, Android and Linux OS with JS [duplicate]

This question already has answers here:
How to detect my browser version and operating system using JavaScript?
(11 answers)
Closed 6 years ago.
How to detect MacOS X, iOS, Windows, Android and Linux operating system with JavaScript?
I learnt a lot about window.navigator object and its properties: platform, appVersion and userAgent. To my mind, it's almost impossible to detect user's OS with 100% sure, but in my case 85%-90% was enough for me.
So, after examining tons of the stackoverflows' answers and some articles, I wrote something like this:
function getOS() {
var userAgent = window.navigator.userAgent,
platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod'],
os = null;
if (macosPlatforms.indexOf(platform) !== -1) {
os = 'Mac OS';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'Windows';
} else if (/Android/.test(userAgent)) {
os = 'Android';
} else if (/Linux/.test(platform)) {
os = 'Linux';
}
return os;
}
alert(getOS());
Inspiration:
What is the list of possible values for navigator.platform as of today?
Best way to detect Mac OS X or Windows computers with JavaScript or jQuery
How to detect my browser version and operating system using JavaScript?
How to detect Browser and Operating System Name and Version using javaScript
Also I used the lists of mobile and desktop browsers to test my code:
List of all Mobile Browsers
List of all Browsers
This code works properly. I tested it on all the OS: MacOS, iOS, Android, Windows and UNIX, but I can't guarantee 100% sure.

Determing iPad and iPhone models with pop up message

Please forgive my lack of knowledge!
I'm interested in creating a script/web application for my company and I've been diligently looking for the answer to my question.
Is it possible to detect iPhone or iPad models with useragent strings and then display a message to the effect of "You have an iPhone 5C"?...
If not, what are my other possible options?
I have seen quite a few posts about detecting if it were an iPad or and iPhone for browser formats.. and Also there is a SDK code that emulates the concept I'm trying to achieve.
I'm low on experience... but keen to learn!
The user-friendly name of the models are not available via API. Apps that I have been involved with that care use data compiled from several websites and build-up some kind of in-app list. You need both a list of models and their user-friendly name equivalents. Both are available via google searches. I don't know of a single, perfectly comprehensive, list, but a bit of searching will find several sites, whose data can be distilled into a single list.
To get the model number of the device to use as a key, the following code is used:
[UIDevice currentDevice].model
I assume you are trying to get the info from a visit to your website. You cannot know exact model of the device with web scripts, at least in my knowledge. But you can determine if the user is using an iPhone, iPad or iPod. Following are two options:
Javascript:
var Apple = {};
Apple.UA = navigator.userAgent;
Apple.Device = false;
Apple.Types = ["iPhone", "iPod", "iPad"];
for (var d = 0; d < Apple.Types.length; d++) {
var t = Apple.Types[d];
Apple[t] = !!Apple.UA.match(new RegExp(t, "i"));
Apple.Device = Apple.Device || Apple[t];
}
// is this an Apple device?
alert(
"Apple device? " + Apple.Device +
"niPhone? " + Apple.iPhone +
"niPod? " + Apple.iPod +
"niPad? " + Apple.iPad
);
PHP script:
// Apple detection array
$Apple = array();
$Apple['UA'] = $_SERVER['HTTP_USER_AGENT'];
$Apple['Device'] = false;
$Apple['Types'] = array('iPhone', 'iPod', 'iPad');
foreach ($Apple['Types'] as $d => $t) {
$Apple[$t] = (strpos($Apple['UA'], $t) !== false);
$Apple['Device'] |= $Apple[$t];
}
// is this an Apple device?
echo
"<p>Apple device? ", ($Apple['Device'] ? 'true' : 'false'),
"</p>n<p>iPhone? ", ($Apple['iPhone'] ? 'true' : 'false'),
"</p>n<p>iPod? ", ($Apple['iPod'] ? 'true' : 'false'),
"</p>n<p>iPad? ", ($Apple['iPad'] ? 'true' : 'false'),
'</p>';
For more information, read here.
Since your tags and statement is a bit confusing, just on the off-chance that If you are using iOS sdk and building an iOS app, you can simply get the model from one line like:
[[UIDevice currentDevice] platformString]

Javascript : is it possible to target only one version of iOs? [duplicate]

This question already has answers here:
Detect iOS version less than 5 with JavaScript
(9 answers)
Closed 9 years ago.
I come across bugs with fixed positionning on iOs (fixed menu on bottom, coming on the middle of screen when the keyboard comes up).
The thing is, this bug only happens on iOs 6.1.4. So the fix i've just done works for iOs 6.1.4 (this one : http://dansajin.com/2012/12/07/fix-position-fixed/), but makes the menu coming to the middle on the screen on iOs 6.1.3...
Is there a way to detect the current version of iOs in JS ?
Have you tried using console.log(navigator); to grab information regarding the browsing machine?
navigator.userAgent or navigator.platform will provide you with the basis for what you need, that said I'm not so sure you'll be able to track it down to such a specific degree.
You can also try this snippet of code:
function iOSversion() {
if (/iP(hone|od|ad)/.test(navigator.platform)) {
// supports iOS 2.0 and later: <http://bit.ly/TJjs1V>
var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
}
}
ver = iOSversion();
if (/(iPad|iPhone|iPod)/g.test( navigator.userAgent ) && ver[0] == 6 && ver[1] ==1 && ver[2]==3) {
alert('This is running iOS 6.1.3 or later.');
}
Adapted from here
My recommendation would be always to attempt to fix the underlying issue rather than implement a workaround, however I appreciate this can sometimes be impractical.
According to this post the iOS version (above 2.0) can be found with:
function iOSversion() {
if (/iP(hone|od|ad)/.test(navigator.platform)) {
// supports iOS 2.0 and later: <http://bit.ly/TJjs1V>
var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
}
}
ver = iOSversion();
if (ver[0] === 6.14) {
alert('This is running iOS 6.14.');
}

What headers or javascript do I add to the HTML to reject older browsers?

Developing a frontend realtime javascript application, we have decided to drop support for older browsers since it takes too much effort to support them.
What header or javascript should we add in the HTML, so that when they hit the URL, I can redirect them a page to obtain newer browsers before proceeding to our application?
you can use following properties of navigator object
navigator.appCodeName
navigator.appName
navigator.appVersion
navigator.cookieEnabled
navigator.platform
navigator.userAgent
you can also use jquery browser object
http://api.jquery.com/jQuery.browser/
The better option is to use feature detection. For example, to test if a browser has geolocation support, you can use the following:
if (navigator.geolocation)
{
// interact with geolocation features
}
If you insist on using browser detection instead, however, you can use the following to detect if the browser is using IE 8 or below:
function getInternetExplorerVersion()
// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
var ver = getInternetExplorerVersion(); // example: 8.0
var belowIE8 = ver <= 8.0;
Found here: http://www.mkyong.com/javascript/how-to-detect-ie-version-using-javascript/
If you want to determine this based on a particular JavaScript version, see below code (untested):
<script language="javascript1.5">
var supported = true;
</script>
<script>
if ('undefined' === typeof supported) {
alert('not at least 1.5 supported');
}
It's still better to do feature detection though, as that ties in better with what you're going to use it for.

Categories

Resources