Created
October 12, 2010 03:09
-
-
Save justinkelly/621607 to your computer and use it in GitHub Desktop.
Javascript mobile OS detector
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
/* | |
* Simple javascript mobile OS / iphone/ android detector | |
* | |
* License: GPLv3+ | |
* Author: justin.kelly.org.au | |
* | |
*/ | |
$(document).ready(function() { | |
var deviceIphone = "iphone"; | |
var deviceIpod = "ipod"; | |
var devicePalm = "palm"; | |
var deviceS60 = "series60"; | |
var deviceSymbian = "symbian"; | |
var engineWebKit = "webkit"; | |
var deviceAndroid = "android"; | |
var deviceWinMob = "windows ce"; | |
var deviceWinPhone = "windows phone"; | |
var deviceBB = "blackberry"; | |
//Initialize our user agent string to lower case. | |
var uagent = navigator.userAgent.toLowerCase(); | |
//************************** | |
// Detects if the current device is an iPhone. | |
function DetectIphone() | |
{ | |
if (uagent.search(deviceIphone) > -1) | |
return true; | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current device is an iPod Touch. | |
function DetectIpod() | |
{ | |
if (uagent.search(deviceIpod) > -1) | |
return true; | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current device is an iPhone or iPod Touch. | |
function DetectIphoneOrIpod() | |
{ | |
if (DetectIphone()) | |
return true; | |
else if (DetectIpod()) | |
return true; | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current browser is the S60 Open Source Browser. | |
// Screen out older devices and the old WML browser. | |
function DetectS60OssBrowser() | |
{ | |
if (uagent.search(engineWebKit) > -1) | |
{ | |
if ((uagent.search(deviceS60) > -1 || | |
uagent.search(deviceSymbian) > -1)) | |
return true; | |
else | |
return false; | |
} | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current device is an Android OS-based device. | |
function DetectAndroid() | |
{ | |
if (uagent.search(deviceAndroid) > -1) | |
return true; | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current device is an Android OS-based device and | |
// the browser is based on WebKit. | |
function DetectAndroidWebKit() | |
{ | |
if (DetectAndroid()) | |
{ | |
if (DetectWebkit()) | |
return true; | |
else | |
return false; | |
} | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current browser is a Windows Mobile device. | |
function DetectWindowsMobile() | |
{ | |
if (uagent.search(deviceWinMob) > -1) | |
return true; | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current browser is a Windows Phone OS 7+ Mobile device. | |
function DetectWindowsPhone() | |
{ | |
if (uagent.search(deviceWinPhone) > -1) | |
return true; | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current browser is a BlackBerry of some sort. | |
function DetectBlackBerry() | |
{ | |
if (uagent.search(deviceBB) > -1) | |
return true; | |
else | |
return false; | |
} | |
//************************** | |
// Detects if the current browser is on a PalmOS device. | |
function DetectPalmOS() | |
{ | |
if (uagent.search(devicePalm) > -1) | |
return true; | |
else | |
return false; | |
} | |
function DetectMobileOS() | |
{ | |
if (DetectIphoneOrIpod() || DetectS60OssBrowser() || DetectAndroid() || DetectAndroidWebKit() || DetectWindowsMobile() || DetectBlackBerry() || DetectPalmOS()) | |
{ | |
return true | |
} else { | |
return false | |
} | |
} | |
if (DetectMobileOS()) | |
{ | |
//do stuff | |
} | |
}); |
thanks spencersugarman - this has been changes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line #149 should read "return false;"