From the below URL, you can get the library source code, which we will be discussing below:
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)
- specific versions (e.g. WebKit)
Usage
Browser
- <script src="mobile-detect.js"></script> <script>
- var md = new MobileDetect(window.navigator.userAgent);
-
Node.js / Express
- var MobileDetect = require('mobile-detect'),
- md = new MobileDetect(req.headers['user-agent']);
-
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');
-
-
-
- console.log( md.mobile() );
-
-
-
-
-
-
-
-
-
-