zoukankan      html  css  js  c++  java
  • isMobile 一个简单的JS库,用来检测移动设备

    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;
                    }
                }
            })();

    Why use isMobile?

    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.

    How it works

    isMobile runs quickly on page load to detect mobile devices; it then creates a JavaScript object with the results.

    Devices detected by isMobile

    The following properies of the isMobile   object will either be   true   or   false

    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
  • 相关阅读:
    SQL中文转拼音
    cocos2D 虚拟摇杆Joystick功能实现
    cocos2d 粒子效果以及Particle Designer粒子工具的学习
    android 模拟器出错,emulator: ERROR: unknown virtual device name
    [转][越狱破解] 苹果itouch 4 iOS5.0.1完美越狱教程+资源下载
    objectivec 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
    [转]cocos2dx添加广告条(IOS and Android)
    cocos2d1.0.1x0.10.0版本 设置横屏与竖屏的方法
    【转】总结阐述Cocos2dX与Cocos2diphone区别;
    Objectivec 中CGGeometry几何类常用方法简单整理
  • 原文地址:https://www.cnblogs.com/wawahaha/p/4537511.html
Copyright © 2011-2022 走看看