Last active
June 27, 2022 18:40
-
-
Save itechnotion/fa95bcf0ff683d3d34a2f71635574792 to your computer and use it in GitHub Desktop.
Javascript to detect OS, browser, ipad, iphone etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define 'is' object and current version | |
let is = {}; | |
is.VERSION = '0.9.0'; | |
// store navigator properties to use later | |
let navigator = window.navigator || {}; | |
let platform = (navigator.platform || '').toLowerCase(); | |
let userAgent = (navigator.userAgent || '').toLowerCase(); | |
let vendor = (navigator.vendor || '').toLowerCase(); | |
// is current browser opera? | |
// parameter is optional | |
is.opera = function(range) { | |
let match = userAgent.match(/(?:^opera.+?version|opr)\/(\d+)/); | |
return match !== null /* && compareVersion(match[1], range) */; | |
}; | |
// is current browser chrome? | |
// parameter is optional | |
is.chrome = function(range) { | |
let match = /google inc/.test(vendor) ? userAgent.match(/(?:chrome|crios)\/(\d+)/) : null; | |
return match !== null && !is.opera() /* && compareVersion(match[1], range) */; | |
}; | |
// is current device iphone? | |
// parameter is optional | |
is.iphone = function(range) { | |
// avoid false positive for Facebook in-app browser on ipad; | |
// original iphone doesn't have the OS portion of the UA | |
let match = is.ipad() ? null : userAgent.match(/iphone(?:.+?os (\d+))?/); | |
return match !== null /* && compareVersion(match[1] || 1, range) */; | |
}; | |
// is current device ipad? | |
// parameter is optional | |
is.ipad = function(range) { | |
let match = userAgent.match(/ipad.+?os (\d+)/); | |
return match !== null /* && compareVersion(match[1], range) */; | |
}; | |
// is current device ipod? | |
// parameter is optional | |
is.ipod = function(range) { | |
let match = userAgent.match(/ipod.+?os (\d+)/); | |
return match !== null /* && compareVersion(match[1], range) */; | |
}; | |
// is current device ios? | |
is.ios = function() { | |
return is.iphone() || is.ipad() || is.ipod(); | |
}; | |
// is current operating system mac? | |
is.mac = function() { | |
return /mac/.test(platform); | |
}; | |
function getChromeVersion () { | |
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./); | |
return raw ? parseInt(raw[2], 10) : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment