IsMobile a simple JS library for detecting mobile devices

Source: Internet
Author: User

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 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 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
      • trueIf 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.