GitHub Address: Https://github.com/kaimallea/isMobile
Example
(function () { var mobile_site=' Http://m.xx.com/index.html ', No_redirect=' Noredirect ';If(IsMobile.Any){if (document. cookie. indexof (No_redirect=== -) {document.= mobile_site }) (
Why use IsMobile?
I had a specific requirement for a project if I created this:
-Redirect all IPhones, iPods, Android phones, and seven inch devices to the mobile site.
A completely separate site had been created for mobile devices, so feature detection/graceful degredation/progressive Enha Ncement were out of the question. I had to redirect.
I couldn ' t do detection on the back-end, because the entire site is cached and served by Akamai; I had to do the detection client-side.
So I resorted to UA sniffing.
I tried to keep the script small ( currently ~1.3k bytes, minified ) and simple, because it would need to execute in the, which are generally a bad idea, since JS blocks downloading and rendering of anything else while it parse S and executes. In the case of mobile redirection, I don ' t mind so much, because I want to start the redirect as soon as possible, before The device has a chance to start downloading and rendering stuff. For Non-mobile platforms, the script should execute fast, so the browser can quickly get back to downloading and rendering .
How it worksIsMobile runs quickly on page load to detect mobile devices; It then creates a JavaScript object with the results.
Devices detected by IsMobileThe following properies of the isMobile
object would either be true
orfalse
Apple devices
isMobile.apple.phone
isMobile.apple.ipod
isMobile.apple.tablet
isMobile.apple.device
(any mobile Apple device)
Android devices
isMobile.android.phone
isMobile.android.tablet
isMobile.android.device
(any mobile Android device)
Windows devices
isMobile.windows.phone
isMobile.windows.tablet
isMobile.windows.device
(any mobile Windows device)
Specific seven inch devices
isMobile.seven_inch
true
If the device is one of the following 7″devices:
- Nexus 7
- Kindle Fire
- Nook Tablet 7 inch
- Galaxy Tab 7 inch
"Other" devices
isMobile.other_blackberry_10
isMobile.other_blackberry
isMobile.other_opera
(Opera Mini)
isMobile.other_firefox
Aggregate groupings
isMobile.any
-Any device matched
isMobile.phone
-Any device in the ' phone ' groups above
isMobile.tablet
-Any device in the ' tablet ' groups above
IsMobile a simple JS library for detecting mobile devices