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.
Big thanks to Igor Ribeio Lima for his contributions.
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 ~740 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.
isMobile runs quickly on page load to detect mobile devices; it then creates a JavaScript object with the results.
The following properies of the isMobile
object will either be true
or false
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)isMobile.windows.phone
isMobile.windows.tablet
isMobile.windows.device
(any mobile Windows device)isMobile.seven_inch
true
if the device is one of the following 7" devices:
isMobile.other_blackberry
isMobile.other_opera
(Opera Mini)isMobile.other_firefox
isMobile.any
- any device matchedisMobile.phone
- any device in the 'phone' groups aboveisMobile.tablet
- any device in the 'tablet' groups aboveI 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 <1kb of JS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
// Minified version of isMobile included in the HTML since it's <1kb
(function(i){var e=/iPhone/i,n=/iPod/i,o=/iPad/i,t=/(?=.*\bAndroid\b)(?=.*\bMobile\b)/i,r=/Android/i,d=/BlackBerry/i,s=/Opera Mini/i,a=/IEMobile/i,b=/(?=.*\bFirefox\b)(?=.*\bMobile\b)/i,h=RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),c=function(i,e){return i.test(e)},l=function(i){var l=i||navigator.userAgent;this.apple={phone:c(e,l),ipod:c(n,l),tablet:c(o,l),device:c(e,l)||c(n,l)||c(o,l)},this.android={phone:c(t,l),tablet:!c(t,l)&&c(r,l),device:c(t,l)||c(r,l)},this.other={blackberry:c(d,l),opera:c(s,l),windows:c(a,l),firefox:c(b,l),device:c(d,l)||c(s,l)||c(a,l)||c(b,l)},this.seven_inch=c(h,l),this.any=this.apple.device||this.android.device||this.other.device||this.seven_inch},v=i.isMobile=new l;v.Class=l})(window);
// 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 and a handful of 7" devices
if (isMobile.apple.phone || isMobile.android.phone || isMobile.seven_inch) {
// 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>
#####Installation
npm install ismobilejs
#####Usage
var isMobile = require('ismobilejs');
console.log(isMobile(req.headers['user-agent']).any);
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.