Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
ismobilejs
Advanced tools
The ismobilejs npm package is a utility library that helps in detecting if a user is accessing a website from a mobile device. It provides a simple API to determine the type of device (mobile, tablet, or desktop) and the operating system being used.
Detect if the device is mobile
This feature allows you to check if the current device is a mobile device. The 'any' property returns true if the device is a mobile device, otherwise false.
const isMobile = require('ismobilejs');
console.log(isMobile().any); // true if the device is mobile, false otherwise
Detect if the device is a tablet
This feature allows you to check if the current device is a tablet. The 'tablet' property returns true if the device is a tablet, otherwise false.
const isMobile = require('ismobilejs');
console.log(isMobile().tablet); // true if the device is a tablet, false otherwise
Detect the operating system
This feature allows you to determine the operating system of the current device. The 'os' property returns the name of the operating system.
const isMobile = require('ismobilejs');
console.log(isMobile().os); // returns the operating system of the device
The mobile-detect package is another utility for detecting mobile devices. It provides more detailed information about the device, including the user agent, phone model, and tablet model. Compared to ismobilejs, it offers more granular detection capabilities.
The detect-mobile-browser package is a lightweight utility for detecting mobile browsers. It is simpler than ismobilejs and focuses solely on identifying if the browser is mobile, without additional details about the device or operating system.
The ua-parser-js package is a comprehensive user-agent string parser that can detect the device type, operating system, and browser. It provides more extensive parsing capabilities compared to ismobilejs, making it suitable for more complex use cases.
A simple JS library that detects mobile devices in both the browser and NodeJS.
You might not need this library. In most cases, responsive design solves the problem of controlling how to render things across different screen sizes. I recommend a mobile first approach. But there are always edge cases. If you have an edge case, then this library might be for you.
My edge case at the time was redirecting users to a completely separate mobile site. I tried to keep this 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 the downloading and rendering of all assets 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 other stuff. For non-mobile platforms, the script should execute fast, so the browser can quickly get back to downloading and rendering.
isMobile runs quickly during initial page load to detect mobile devices; it then creates a JavaScript object with the results.
You might want to use this library to do server-side device detection to minimize the amount of bytes you send back to visitors. Or you have your own arbitrary use case.
You import and call the isMobile
function, passing it a user agent string; it then returns a JavaScript object with the results.
In a browser, the following properties of the global isMobile
object will either be true
or false
. In Node, isMobile
will be whatever you named the variable.
isMobile.apple.phone
isMobile.apple.ipod
isMobile.apple.tablet
isMobile.apple.device
(any mobile Apple device)isMobile.android.phone
isMobile.android.tablet
isMobile.android.device
(any mobile Android device; OkHttp user agents will match this)isMobile.amazon.phone
isMobile.amazon.tablet
isMobile.amazon.device
(any mobile Amazon Silk device)isMobile.windows.phone
isMobile.windows.tablet
isMobile.windows.device
(any mobile Windows device)isMobile.other.blackberry_10
isMobile.other.blackberry
isMobile.other.opera
(Opera Mini)isMobile.other.firefox
isMobile.other.chrome
isMobile.other.device
(any "Other" device)isMobile.any
- any device matchedisMobile.phone
- any device in the 'phone' groups aboveisMobile.tablet
- any device in the 'tablet' groups aboveyarn install ismobilejs
import isMobile from 'ismobilejs';
const userAgent = req.headers['user-agent'];
console.log(isMobile(userAgent).any);
A real-word example: I include the minified version of the script, inline, and at the top of the <head>
. Cellular connections tend to suck, so it would be wasteful overhead to open another connection, just to download ~1.3kb of JS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script>
// Minified version of isMobile included in the HTML since it's small
(function () {var f=/iPhone/i,j=/iPod/i,p=/iPad/i,g=/\bAndroid(?:.+)Mobile\b/i,i=/Android/i,d=/\bAndroid(?:.+)SD4930UR\b/i,e=/\bAndroid(?:.+)(?:KF[A-Z]{2,4})\b/i,c=/Windows Phone/i,h=/\bWindows(?:.+)ARM\b/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/\b(CriOS|Chrome)(?:.+)Mobile/i,o=/Mobile(?:.+)Firefox\b/i;function b($,a){return $.test(a)}function a($){var a=($=$||("undefined"!=typeof navigator?navigator.userAgent:"")).split("[FBAN");void 0!==a[1]&&($=a[0]),void 0!==(a=$.split("Twitter"))[1]&&($=a[0]);var r={apple:{phone:b(f,$)&&!b(c,$),ipod:b(j,$),tablet:!b(f,$)&&b(p,$)&&!b(c,$),device:(b(f,$)||b(j,$)||b(p,$))&&!b(c,$)},amazon:{phone:b(d,$),tablet:!b(d,$)&&b(e,$),device:b(d,$)||b(e,$)},android:{phone:!b(c,$)&&b(d,$)||!b(c,$)&&b(g,$),tablet:!b(c,$)&&!b(d,$)&&!b(g,$)&&(b(e,$)||b(i,$)),device:!b(c,$)&&(b(d,$)||b(e,$)||b(g,$)||b(i,$))||b(/\bokhttp\b/i,$)},windows:{phone:b(c,$),tablet:b(h,$),device:b(c,$)||b(h,$)},other:{blackberry:b(k,$),blackberry10:b(l,$),opera:b(m,$),firefox:b(o,$),chrome:b(n,$),device:b(k,$)||b(l,$)||b(m,$)||b(o,$)||b(n,$)},any:!1,phone:!1,tablet:!1};return r.any=r.apple.device||r.android.device||r.windows.device||r.other.device,r.phone=r.apple.phone||r.android.phone||r.windows.phone,r.tablet=r.apple.tablet||r.android.tablet||r.windows.tablet,r}window.isMobile=a();})();
// My own arbitrary use of isMobile, as an example
(function() {
var MOBILE_SITE = '/mobile/index.html', // site to redirect to
NO_REDIRECT = 'noredirect'; // cookie to prevent redirect
// I only want to redirect iPhones, Android phones
if (isMobile.apple.phone || isMobile.android.phone) {
// Only redirect if the user didn't previously choose
// to explicitly view the full site. This is validated
// by checking if a "noredirect" cookie exists
if (document.cookie.indexOf(NO_REDIRECT) === -1) {
document.location = MOBILE_SITE;
}
}
})();
</script>
</head>
<body>
<!-- imagine lots of html and content -->
</body>
</html>
Alternatively, you can include this library via jsDelivr CDN in a script
tag:
<script src="https://cdn.jsdelivr.net/npm/ismobilejs@1/dist/isMobile.min.js"></script>
Visit the isMobile jsDelivr page to get the most up-to-date URL pointing to the lastest version.
After checking out the repo, install dependencies:
yarn install
Then build the library:
yarn build
Three versions of the library will be generated:
./cjs/index.js
- the CommonJS version of the library./esm/index.js
- the ESModule version of the library./dist/isMobile.min.js
- the browser version of the libraryAdditionally, types will be output to types
.
This library uses Spotify's web-scripts project to build, lint, test, format and release the this library.
You must use yarn commit
rather than git commit
to commit files. This enforced commit messages to following a specific format and enables automation of release notes and version bump.
FAQs
A simple JS library that detects mobile devices.
The npm package ismobilejs receives a total of 204,275 weekly downloads. As such, ismobilejs popularity was classified as popular.
We found that ismobilejs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.