Skip to content

Instantly share code, notes, and snippets.

@meiwin
Created July 2, 2014 03:09
Show Gist options
  • Save meiwin/de528ad276b11f3726de to your computer and use it in GitHub Desktop.
Save meiwin/de528ad276b11f3726de to your computer and use it in GitHub Desktop.
Browser Check
<html>
<head>
</head>
<body>
<script type="text/javascript">
navigator.os = (function() {
var ua = navigator.userAgent, tem,
M= ua.match(/.*\((.+?);.*?([012356789_\.]+).*?\).*/i) || [];
return M[1];
}());
navigator.os_version = (function() {
var ua = navigator.userAgent, tem,
M= ua.match(/.*\((.+?);.*?([012356789_\.]+).*?\).*/i) || [];
return M[2];
}());
function requireOS(os, os_version, fn_success, fn_error) {
var thisOs = navigator.os.toLowerCase();
var requiredOs = os.toLowerCase();
if (thisOs != requiredOs) {
if (fn_error) fn_error();
return;
}
else {
var thisOsVersion = navigator.os_version.indexOf("_") > -1 ? navigator.os_version.split("_") : navigator.os_version.split(".");
var requiredOsVersion = os_version.indexOf("_") > -1 ? os_version.split("_") : os_version.split(".");
for (var i = 0; i < requiredOsVersion.length; i++) {
if (thisOsVersion.length == i) { /* ended before managed to check all requirements */
if (fn_error) fn_error();
return;
}
var a = requiredOsVersion[i];
var b = thisOsVersion[i];
if (b > a) {
if (fn_success) fn_success();
return;
} else if (b < a) {
if (fn_error) fn_error();
return;
}
}
if (fn_success) fn_success();
}
}
window.onload = function() {
requireOS('ipad', '7', function() {
alert('yeah');
}, function() {
alert('meh');
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment