From the below URL, you can get the library source code, which we will be discussing below:
http://hgoebl.github.io/mobile-detect.js/
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
  1. <script src="mobile-detect.js"></script> <script>
  2. var md = new MobileDetect(window.navigator.userAgent);
  3. // ... see below </script>
Node.js / Express
  1. var MobileDetect = require('mobile-detect'),
  2. md = new MobileDetect(req.headers['user-agent']);
  3. // ... see below
General
  1. var md = new MobileDetect('Mozilla/5.0 (Linux; U; Android 4.0.3; en-in; SonyEricssonMT11i' +
  2. ' Build/4.1.A.0.562) AppleWebKit/534.30 (KHTML, like Gecko)'
  3. + ' Version/4.0 Mobile Safari/534.30');
  4. // more typically we would instantiate with 'window.navigator.userAgent'
  5. // as user-agent; this string literal is only for better understanding
  6. console.log( md.mobile() );
  7. // 'Sony' console.log( md.phone() );
  8. // 'Sony' console.log( md.tablet() );
  9. // null console.log( md.userAgent() );
  10. // 'Safari' console.log( md.os() );
  11. // 'AndroidOS' console.log( md.is('iPhone') );
  12. // false console.log( md.is('bot') );
  13. // false console.log( md.version('Webkit') );
  14. // 534.3 console.log( md.versionStr('Build') );
  15. // '4.1.A.0.562' console.log( md.match('playstation|xbox') );
  16. // false