mobile-detect vs what-input vs modernizr
Web Development Utility Libraries Comparison
1 Year
mobile-detectwhat-inputmodernizr
What's Web Development Utility Libraries?

These libraries serve distinct purposes in web development, focusing on device detection, feature detection, and input handling respectively. 'mobile-detect' is designed to identify mobile devices and their capabilities, 'modernizr' detects HTML5 and CSS3 features in the user's browser, allowing developers to implement fallbacks for unsupported features, while 'what-input' helps manage input events and improve accessibility by detecting the type of input method being used (mouse, keyboard, touch). Together, they enhance the adaptability and responsiveness of web applications across various devices and input methods.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
mobile-detect187,9374,135-134 years agoMIT
what-input119,7701,36583.6 kB16-MIT
modernizr50,75125,726441 kB1929 months agoMIT
Feature Comparison: mobile-detect vs what-input vs modernizr

Device Detection

  • mobile-detect:

    Mobile-detect provides a simple API to detect mobile devices based on the User-Agent string. It can identify specific devices, operating systems, and browsers, allowing developers to tailor content and functionality for mobile users effectively.

  • what-input:

    What-input does not perform device detection in the traditional sense but rather detects the type of input method being used (mouse, keyboard, or touch). This allows developers to adjust interactions and styles based on how the user is interacting with the application.

  • modernizr:

    Modernizr does not focus on device detection but rather on feature detection. It checks for the availability of specific HTML5 and CSS3 features in the user's browser, enabling developers to implement fallbacks or alternative solutions if certain features are not supported.

Feature Detection

  • mobile-detect:

    Mobile-detect does not provide feature detection capabilities; its primary focus is on identifying device types. It can be used in conjunction with other libraries like Modernizr for comprehensive feature detection.

  • what-input:

    What-input does not provide traditional feature detection but can be used to enhance user experience by adapting the interface based on the detected input method, thus improving accessibility.

  • modernizr:

    Modernizr excels in feature detection, allowing developers to check for the presence of HTML5 and CSS3 features in the user's browser. This enables the use of progressive enhancement techniques, ensuring that users have a functional experience regardless of their browser capabilities.

Usability

  • mobile-detect:

    Mobile-detect is straightforward to use, requiring minimal setup. It provides a simple API that can be easily integrated into existing projects to enhance mobile responsiveness and functionality.

  • what-input:

    What-input is easy to implement and can be integrated into any project with minimal configuration. It enhances usability by allowing developers to adapt their applications based on user input methods.

  • modernizr:

    Modernizr requires some initial setup, including defining which features to test for. However, once configured, it provides a powerful way to manage browser compatibility and feature support across different environments.

Performance Impact

  • mobile-detect:

    Mobile-detect has a minimal performance impact as it primarily relies on User-Agent string parsing, which is generally lightweight. However, overuse of device-specific logic can lead to maintenance challenges.

  • what-input:

    What-input is lightweight and has a negligible performance impact. It runs efficiently, detecting input methods without causing noticeable delays in application performance.

  • modernizr:

    Modernizr can have a slight performance impact due to its feature detection processes, especially if many features are being tested. However, it significantly improves user experience by ensuring compatibility with various browsers.

Community and Support

  • mobile-detect:

    Mobile-detect has a moderate community and support base, with documentation available for common use cases. However, it may not be as widely used as some other libraries, which could limit community-driven resources.

  • what-input:

    What-input has a smaller community compared to Modernizr but is well-documented. It is gaining popularity for its focus on input management, which may lead to increased community support over time.

  • modernizr:

    Modernizr has a strong community and extensive documentation, making it easy to find support and examples. It is widely used in the industry, ensuring a wealth of resources and best practices are available.

How to Choose: mobile-detect vs what-input vs modernizr
  • mobile-detect:

    Choose 'mobile-detect' if your application requires specific adaptations for mobile devices, such as altering layouts or functionality based on whether the user is on a mobile or desktop device.

  • what-input:

    Opt for 'what-input' if your application needs to handle different input methods effectively, improving accessibility and user experience by tailoring interactions based on whether users are using a keyboard, mouse, or touch.

  • modernizr:

    Select 'modernizr' if your project needs to ensure compatibility with various browsers by detecting available features and applying polyfills or fallbacks for unsupported features, enhancing the overall user experience.

README for mobile-detect

mobile-detect.js

A loose port of Mobile-Detect to JavaScript.

This script will detect the device by comparing patterns against a given User-Agent string. You can find out information about the device rendering your web page:

  • mobile or not
  • if mobile, whether phone or tablet
  • operating system
  • Mobile Grade (A, B, C) REMARK: this is completely outdated: all current devices will return an 'A', so it's useless nowadays
  • specific versions (e.g. WebKit)

Current master branch is using detection logic from Mobile-Detect@2.8.37

Live Demo

Demo/check (sorry about the missing styling) can be found here.

Warning

TL;DR: you should not use this library in your HTML page and it's less reliable when used server-side (Node.js)

As mentioned later, "User-Agent" based detection is not a reliable solution in most cases, because:

  • The rules (regular expressions) are constantly out-dated and incomplete
  • You have to update the detection code continuously
  • There are other ways to detect how your web application should behave:
  • Maybe there are some libraries out there (which are probably not free) doing a more reliable job

If you still want to (or have to) use this library, you should always encapsulate it with your own code, because chances a very high that you have to tweak the behaviour a bit or are not satisfied with the result of mobile-detect.js. Don't spread usage of MobileDetect all over your own code! As you can see in the issues, there are some "bugs", feature-requests, pull-requests where people are not so happy how MobileDetect works. But I cannot change its behaviour from version to version, even if this was reasonable from new users' point of view. I hope you show understanding.

At least there is a way to monkey-patch the library (see "Extending" below).

Usage

Browser

<script src="mobile-detect.js"></script>
<script>
    var md = new MobileDetect(window.navigator.userAgent);
    // ... see below
</script>

Node.js / Express

var MobileDetect = require('mobile-detect'),
    md = new MobileDetect(req.headers['user-agent']);
// ... see below

General

var md = new MobileDetect(
    'Mozilla/5.0 (Linux; U; Android 4.0.3; en-in; SonyEricssonMT11i' +
    ' Build/4.1.A.0.562) AppleWebKit/534.30 (KHTML, like Gecko)' +
    ' Version/4.0 Mobile Safari/534.30');

// more typically we would instantiate with 'window.navigator.userAgent'
// as user-agent; this string literal is only for better understanding

console.log( md.mobile() );          // 'Sony'
console.log( md.phone() );           // 'Sony'
console.log( md.tablet() );          // null
console.log( md.userAgent() );       // 'Safari'
console.log( md.os() );              // 'AndroidOS'
console.log( md.is('iPhone') );      // false
console.log( md.is('bot') );         // false
console.log( md.version('Webkit') );         // 534.3
console.log( md.versionStr('Build') );       // '4.1.A.0.562'
console.log( md.match('playstation|xbox') ); // false

More Info ...

There is some documentation generated by JSDoc:

https://hgoebl.github.io/mobile-detect.js/doc/MobileDetect.html

Side Effects

Script creates the global property MobileDetect.

Modernizr Extension

When using Modernizr, you can include mobile-detect-modernizr.js. It will add the CSS classes mobile, phone, tablet and mobilegradea if applicable.

You can easily extend it, e.g. android, iphone, etc.

Size (bytes)

  • development: 70168
  • minified: 39585
  • minified + gzipped: 16556 (cat mobile-detect.min.js | gzip -9f | wc -c)

Installation

Bower (which you should not use for new projects)

$ bower install hgoebl/mobile-detect.js --save

Node.js / npm

$ npm install mobile-detect --save

CDN - jsDelivr

<script src="https://cdn.jsdelivr.net/npm/mobile-detect@1.4.5/mobile-detect.min.js"></script>

cdnjs - cdnjs.com

<script src="https://cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.4.5/mobile-detect.min.js"></script>

Extending/Modifying Behaviour

Though it is not recommended to rely on internal methods or structures of MobileDetect, you can alter the behaviour by replacing particular internal methods with your own implementations. If you feel like this is the only possibility, then go ahead and have a look at the source code and examples in tests/spec/MobileDetectSpec.js (search for "Extensibility").

Alternatives / Infos

Often device detection is the first solution in your mind. Please consider looking for other solutions like media queries and feature detection (e.g. w/ Modernizr). Maybe there are better (simpler, smaller, faster) device detection libraries, so here you have a list (order has no meaning apart from first element):

Mobile-Usage Statistics

If you have to provide statistics about how many and which mobile devices are hitting your web-site, you can generate statistics (data and views) with hgoebl/mobile-usage. There are many hooks to customize statistical calculation to your needs.

License

MIT-License (see LICENSE file).

Contributing

Your contribution is welcome. If you want new devices to be supported, please contribute to Mobile-Detect instead.

To run generate-script it is necessary to have Mobile-Detect as a sibling directory to mobile-detect.js/. (I tried to use git subtree but had some problems on Mac OS X - probably my fault...)

  • fork or clone serbanghita/Mobile-Detect
  • fork hgoebl/mobile-detect.js
  • run npm install
  • create branch
  • make changes and run grunt (needs PHP >= 5.4 in your path)
  • run browser test (tests/SpecRunner.html)
  • commit, push to your branch
  • create pull request

Testing

Browser

Open tests/SpecRunner.html in different browsers.

Node.js

$ npm test
$ # or
$ grunt jasmine_node

Donations

If you want, you can donate to Mobile-Detect.

TODO

  • Extend RegEx patterns so that test passes
  • support ES6 modules