What is is-mobile?
The is-mobile npm package is a lightweight utility that helps in detecting if a user is accessing a website from a mobile device. It can be used to tailor user experiences based on the type of device being used.
What are is-mobile's main functionalities?
Basic Mobile Detection
This feature allows you to detect if the user is accessing the website from a mobile device. The function returns a boolean value.
const isMobile = require('is-mobile');
if (isMobile()) {
console.log('User is on a mobile device');
} else {
console.log('User is not on a mobile device');
}
Custom User-Agent String
This feature allows you to pass a custom User-Agent string to the isMobile function to determine if it corresponds to a mobile device.
const isMobile = require('is-mobile');
const userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14E5239e Safari/602.1';
if (isMobile({ ua: userAgent })) {
console.log('User-Agent indicates a mobile device');
} else {
console.log('User-Agent does not indicate a mobile device');
}
Custom HTTP Headers
This feature allows you to pass custom HTTP headers to the isMobile function to determine if they indicate a mobile device.
const isMobile = require('is-mobile');
const headers = { 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14E5239e Safari/602.1' };
if (isMobile({ headers })) {
console.log('HTTP headers indicate a mobile device');
} else {
console.log('HTTP headers do not indicate a mobile device');
}
Other packages similar to is-mobile
mobile-detect
The mobile-detect package provides more detailed information about the device, including the type of mobile device, operating system, and user-agent. It offers more granular control compared to is-mobile.
detect-mobile-browser
The detect-mobile-browser package is another lightweight utility for detecting mobile browsers. It is similar to is-mobile but offers fewer customization options.
ua-parser-js
The ua-parser-js package is a comprehensive library for parsing user-agent strings. It provides detailed information about the browser, engine, OS, and device, making it more versatile than is-mobile.
is-mobile
Check if mobile browser, based on useragent string.
Example
var mobile = require('is-mobile');
console.log(mobile());
API
mobile({ [ua], [tablet], [featureDetect] })
Returns true if a mobile browser is being used.
If you don't specify opts.ua
it will use navigator.userAgent
.
To add support for tablets, set tablet: true
.
To enable feature detection (i.e. namely for iPad with iOS 13), set featureDetect: true
and tablet: true
. This will only work in browser environments.
opts.ua
can also be an instance of a node.js http request, in which
case it will reader the user agent header.
Example:
var http = require('http');
var mobile = require('is-mobile');
var server = http.createServer(function (req, res) {
res.end(mobile(req));
});
server.listen(8000);
Installation
With npm do:
npm install is-mobile
Bundle for the browser with
browserify.
Kudos
Taken from detectmobilebrowsers.com.
License
(MIT)
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.