Mobile, iOS, Safari, IE browser detection using simple JS

This post is just for reference for when browser detections are required:


/*If handheld device such as mobile and tablets returns true, else false */
function isHandheldDevice() {
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
return true;
}
return false;
}


/*Simple: If safari on windows returns true, else false */
function isSafariOnWindows() {
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("safari/") !== -1 && // It says it's Safari
ua.indexOf("windows") !== -1 && // It says it's on Windows
ua.indexOf("chrom") === -1 // It DOESN'T say it's Chrome/Chromium
) {
return true;
}
return false;
}


/*Advanced: If safari on windows returns true, else false */
function isSafariNotMac () {
var myNav = navigator.userAgent.toLowerCase();
var is_chrome = myNav.indexOf('chrome') > -1;
var is_safari = myNav.indexOf("safari") > -1;
var is_mac = myNav.indexOf("macintosh") > -1;
var is_windows = myNav.indexOf("windows") > -1;
if ((is_chrome)&&(is_safari)) {is_safari=false;}
if ((is_mac)&&(is_safari)) {is_safari=false;}
if ((is_windows)&&(is_safari)) {is_safari=false;}
return is_safari;
}


/* If Safari return version number, else return false */
function safariVersion()
{
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("safari/") !== -1 && ua.indexOf("chrom")   === -1 )
{
var version = parseInt(ua.substr(ua.indexOf("safari/") + ("safari/").length, 1));
return version;
}
return false;
}


/* If IE return version number, else return false */
function isIE() {
var myNav = navigator.userAgent.toLowerCase();
return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s