Skip to content

Instantly share code, notes, and snippets.

@ahmed-musallam
Last active September 10, 2019 04:23
Show Gist options
  • Save ahmed-musallam/d5ade4ba7ef92accc86d7bfe74c11a06 to your computer and use it in GitHub Desktop.
Save ahmed-musallam/d5ade4ba7ef92accc86d7bfe74c11a06 to your computer and use it in GitHub Desktop.

Revisions

  1. ahmed-musallam revised this gist Sep 10, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion device-detector.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    /***
    * a trimmed down version of: https://github.com/matthewhudson/current-device/blob/master/src/index.js
    *
    * a trimmed down version (~812 bytes minified, not gzipped) of: https://github.com/matthewhudson/current-device/blob/master/src/index.js
    * to determine current device is mobile, tablet or desktop
    * USAGE:
    * 1. prebuilt property: `window.device.type` returns "mobile", "tablet" or "desktop"
  2. ahmed-musallam created this gist Sep 10, 2019.
    54 changes: 54 additions & 0 deletions device-detector.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    /***
    * a trimmed down version of: https://github.com/matthewhudson/current-device/blob/master/src/index.js
    * to determine current device is mobile, tablet or desktop
    * USAGE:
    * 1. prebuilt property: `window.device.type` returns "mobile", "tablet" or "desktop"
    * 2. could call `window.device.mobile()` or `window.device.tablet()` or `window.device.desktop()` to check for each
    * 3. other methods such as `window.device.windows()`, `window.device.windowsPhone()`, `window.device.android()`, `window.device.blackberry()`
    */

    (function() {
    var uAgent = window.navigator.userAgent.toLowerCase();
    function find(needle) { return uAgent.indexOf(needle) !== -1; }

    window.device = {
    windows: function() { return find('windows'); },
    windowsPhone: function() { return this.windows() && find('phone'); },
    android: function() { return !this.windows() && find('android'); },
    blackberry: function() { return find('blackberry') || find('bb10') || find('rim'); },
    fxos: function() { return (find('(mobile') || find('(tablet')) && find(' rv:');},
    mobile: function() {
    return (
    (this.android() && find('mobile')) ||
    (!this.windows() && find('iphone')) ||
    find('ipod') ||
    (this.windows() && find('phone')) ||
    (this.blackberry() && !find('tablet')) ||
    (this.fxos() && find('mobile')) ||
    find('meego')
    );
    },
    tablet: function() {
    return (
    find('ipad') ||
    (this.android() && !find('mobile')) ||
    (this.blackberry() && find('tablet')) ||
    (this.windows() && (find('touch') && !this.windowsPhone())) ||
    (this.fxos() && find('tablet'))
    );
    },
    desktop: function() {
    return !this.tablet() && !this.mobile();
    },
    findMatch: function (arr) {
    for (let i = 0; i < arr.length; i++) {
    if (this[arr[i]]()) {
    return arr[i];
    }
    }
    return 'unknown';
    }
    };
    var type = device.findMatch(['mobile', 'tablet', 'desktop']);
    window.device.type = type;
    })();