isMobile 一個簡單的JS庫,用來檢測行動裝置

來源:互聯網
上載者:User

標籤:

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

isMobile 一個簡單的JS庫,用來檢測行動裝置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.