github地址: https://github.com/kaimallea/isMobile
(function () {
var MOBILE_SITE = ‘http://m.xx.com/index.html‘,
NO_REDIRECT = ‘noredirect‘;
if (isMobile.any) {
if ( document.cookie.indexOf(NO_REDIRECT) === -1 ) {
document.location = MOBILE_SITE;
}
}
})();
I had a specific requirement for a project when 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 enhancement were out of the question. I had to redirect.
I couldn’t do detection on the back-end, because the entire site was 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 <head> , which is generally a bad idea, since JS blocks downloading and rendering of anything else while it parses 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.
isMobile runs quickly on page load to detect mobile devices; it then creates a JavaScript object with the results.
The following properies of the isMobile object will either be true or false
isMobile.apple.phoneisMobile.apple.ipodisMobile.apple.tabletisMobile.apple.device (any mobile Apple device)isMobile.android.phoneisMobile.android.tabletisMobile.android.device (any mobile Android device)isMobile.windows.phoneisMobile.windows.tabletisMobile.windows.device (any mobile Windows device)isMobile.seven_inch
true if the device is one of the following 7″ devices:
isMobile.other_blackberry_10isMobile.other_blackberryisMobile.other_opera (Opera Mini)isMobile.other_firefoxisMobile.any - any device matchedisMobile.phone - any device in the ‘phone’ groups aboveisMobile.tablet - any device in the ‘tablet’ groups above原文:http://www.cnblogs.com/wawahaha/p/4537511.html