Class: MobileDetect

MobileDetect(userAgent, maxPhoneWidthopt)

new MobileDetect(userAgent, maxPhoneWidthopt)

Constructor for MobileDetect object.
Such an object will keep a reference to the given user-agent string and cache most of the detect queries.
Find information how to download and install: github.com/hgoebl/mobile-detect.js/
Parameters:
Name Type Attributes Default Description
userAgent string typically taken from window.navigator.userAgent or http_header['User-Agent']
maxPhoneWidth number <optional>
600 only for browsers specify a value for the maximum width of smallest device side (in logical "CSS" pixels) until a device detected as mobile will be handled as phone. This is only used in cases where the device cannot be classified as phone or tablet.
See Declaring Tablet Layouts for Android.
If you provide a value < 0, then this "fuzzy" check is disabled.
Source:
Example
<pre>
    var md = new MobileDetect(window.navigator.userAgent);
    if (md.mobile()) {
        location.href = (md.mobileGrade() === 'A') ? '/mobile/' : '/lynx/';
    }
</pre>

Methods

is(key) → {boolean}

Global test key against userAgent, os, phone, tablet and some other properties of userAgent string.
Parameters:
Name Type Description
key String the key (case-insensitive) of a userAgent, an operating system, phone or tablet family.
For a complete list of possible values, see MobileDetect#userAgent, MobileDetect#os, MobileDetect#phone, MobileDetect#tablet.
Additionally you have following keys:

Bot, MobileBot, DesktopMode, TV, WebKit, Console, Watch
Source:
Returns:
true when the given key is one of the defined keys of userAgent, os, phone, tablet or one of the listed additional keys, otherwise false
Type
boolean

match(pattern) → {boolean}

Do a quick test against navigator::userAgent.
Parameters:
Name Type Description
pattern String | RegExp the pattern, either as String or RegExp (a string will be converted to a case-insensitive RegExp).
Source:
Returns:
true when the pattern matches, otherwise false
Type
boolean

mobile() → {String}

Returns the detected phone or tablet type or null if it is not a mobile device.
For a list of possible return values see MobileDetect#phone and MobileDetect#tablet.

If the device is not detected by the regular expressions from Mobile-Detect, a test is made against the patterns of detectmobilebrowsers.com. If this test is positive, a value of UnknownPhone, UnknownTablet or UnknownMobile is returned.
When used in browser, the decision whether phone or tablet is made based on screen.width/height.

When used server-side (node.js), there is no way to tell the difference between UnknownTablet and UnknownMobile, so you will get UnknownMobile here.
Be aware that since v1.0.0 in this special case you will get UnknownMobile only for: MobileDetect#mobile, not for MobileDetect#phone and MobileDetect#tablet. In versions before v1.0.0 all 3 methods returned UnknownMobile which was tedious to use.
In most cases you will use the return value just as a boolean.
Source:
Returns:
the key for the phone family or tablet family, e.g. "Nexus".
Type
String

mobileGrade() → {String}

Returns the mobile grade ('A', 'B', 'C').
Source:
Returns:
one of the mobile grades ('A', 'B', 'C').
Type
String

os() → {String}

Returns the detected operating system string or null.
The operating system is one of following keys:

AndroidOS, BlackBerryOS, PalmOS, SymbianOS, WindowsMobileOS, WindowsPhoneOS, iOS, iPadOS, SailfishOS, MeeGoOS, MaemoOS, JavaOS, webOS, badaOS, BREWOS
Source:
Returns:
the key for the detected operating system.
Type
String

phone() → {String}

Returns the detected phone type/family string or null.
The returned tablet (family or producer) is one of following keys:

iPhone, BlackBerry, Pixel, HTC, Nexus, Dell, Motorola, Samsung, LG, Sony, Asus, Xiaomi, NokiaLumia, Micromax, Palm, Vertu, Pantech, Fly, Wiko, iMobile, SimValley, Wolfgang, Alcatel, Nintendo, Amoi, INQ, OnePlus, GenericPhone

If the device is not detected by the regular expressions from Mobile-Detect, a test is made against the patterns of detectmobilebrowsers.com. If this test is positive, a value of UnknownPhone or UnknownMobile is returned.
When used in browser, the decision whether phone or tablet is made based on screen.width/height.

When used server-side (node.js), there is no way to tell the difference between UnknownTablet and UnknownMobile, so you will get null here, while MobileDetect#mobile will return UnknownMobile.
Be aware that since v1.0.0 in this special case you will get UnknownMobile only for: MobileDetect#mobile, not for MobileDetect#phone and MobileDetect#tablet. In versions before v1.0.0 all 3 methods returned UnknownMobile which was tedious to use.
In most cases you will use the return value just as a boolean.
Source:
Returns:
the key of the phone family or producer, e.g. "iPhone"
Type
String

tablet() → {String}

Returns the detected tablet type/family string or null.
The returned tablet (family or producer) is one of following keys:

iPad, NexusTablet, GoogleTablet, SamsungTablet, Kindle, SurfaceTablet, HPTablet, AsusTablet, BlackBerryTablet, HTCtablet, MotorolaTablet, NookTablet, AcerTablet, ToshibaTablet, LGTablet, FujitsuTablet, PrestigioTablet, LenovoTablet, DellTablet, YarvikTablet, MedionTablet, ArnovaTablet, IntensoTablet, IRUTablet, MegafonTablet, EbodaTablet, AllViewTablet, ArchosTablet, AinolTablet, NokiaLumiaTablet, SonyTablet, PhilipsTablet, CubeTablet, CobyTablet, MIDTablet, MSITablet, SMiTTablet, RockChipTablet, FlyTablet, bqTablet, HuaweiTablet, NecTablet, PantechTablet, BronchoTablet, VersusTablet, ZyncTablet, PositivoTablet, NabiTablet, KoboTablet, DanewTablet, TexetTablet, PlaystationTablet, TrekstorTablet, PyleAudioTablet, AdvanTablet, DanyTechTablet, GalapadTablet, MicromaxTablet, KarbonnTablet, AllFineTablet, PROSCANTablet, YONESTablet, ChangJiaTablet, GUTablet, PointOfViewTablet, OvermaxTablet, HCLTablet, DPSTablet, VistureTablet, CrestaTablet, MediatekTablet, ConcordeTablet, GoCleverTablet, ModecomTablet, VoninoTablet, ECSTablet, StorexTablet, VodafoneTablet, EssentielBTablet, RossMoorTablet, iMobileTablet, TolinoTablet, AudioSonicTablet, AMPETablet, SkkTablet, TecnoTablet, JXDTablet, iJoyTablet, FX2Tablet, XoroTablet, ViewsonicTablet, VerizonTablet, OdysTablet, CaptivaTablet, IconbitTablet, TeclastTablet, OndaTablet, JaytechTablet, BlaupunktTablet, DigmaTablet, EvolioTablet, LavaTablet, AocTablet, MpmanTablet, CelkonTablet, WolderTablet, MediacomTablet, MiTablet, NibiruTablet, NexoTablet, LeaderTablet, UbislateTablet, PocketBookTablet, KocasoTablet, HisenseTablet, Hudl, TelstraTablet, GenericTablet

If the device is not detected by the regular expressions from Mobile-Detect, a test is made against the patterns of detectmobilebrowsers.com. If this test is positive, a value of UnknownTablet or UnknownMobile is returned.
When used in browser, the decision whether phone or tablet is made based on screen.width/height.

When used server-side (node.js), there is no way to tell the difference between UnknownTablet and UnknownMobile, so you will get null here, while MobileDetect#mobile will return UnknownMobile.
Be aware that since v1.0.0 in this special case you will get UnknownMobile only for: MobileDetect#mobile, not for MobileDetect#phone and MobileDetect#tablet. In versions before v1.0.0 all 3 methods returned UnknownMobile which was tedious to use.
In most cases you will use the return value just as a boolean.
Source:
Returns:
the key of the tablet family or producer, e.g. "SamsungTablet"
Type
String

userAgent() → {String}

Returns the (first) detected user-agent string or null.
The returned user-agent is one of following keys:

Chrome, Dolfin, Opera, Skyfire, Edge, IE, Firefox, Bolt, TeaShark, Blazer, Safari, WeChat, UCBrowser, baiduboxapp, baidubrowser, DiigoBrowser, Mercury, ObigoBrowser, NetFront, GenericBrowser, PaleMoon

In most cases calling MobileDetect#userAgent will be sufficient. But there are rare cases where a mobile device pretends to be more than one particular browser. You can get the list of all matches with MobileDetect#userAgents or check for a particular value by providing one of the defined keys as first argument to MobileDetect#is.
Source:
Returns:
the key for the detected user-agent or null
Type
String

userAgents() → {Array}

Returns all detected user-agent strings.
The array is empty or contains one or more of following keys:

Chrome, Dolfin, Opera, Skyfire, Edge, IE, Firefox, Bolt, TeaShark, Blazer, Safari, WeChat, UCBrowser, baiduboxapp, baidubrowser, DiigoBrowser, Mercury, ObigoBrowser, NetFront, GenericBrowser, PaleMoon

In most cases calling MobileDetect#userAgent will be sufficient. But there are rare cases where a mobile device pretends to be more than one particular browser. You can get the list of all matches with MobileDetect#userAgents or check for a particular value by providing one of the defined keys as first argument to MobileDetect#is.
Source:
Returns:
the array of detected user-agent keys or []
Type
Array

version(key) → {Number}

Get the version (as Number) of the given property in the User-Agent.
Will return a float number. (eg. 2_0 will return 2.0, 4.3.1 will return 4.31)
Parameters:
Name Type Description
key String a key defining a thing which has a version.
You can use one of following keys:

Mobile, Build, Version, VendorID, iPad, iPhone, iPod, Kindle, Chrome, Coast, Dolfin, Firefox, Fennec, Edge, IE, NetFront, NokiaBrowser, Opera, Opera Mini, Opera Mobi, UCBrowser, MQQBrowser, MicroMessenger, baiduboxapp, baidubrowser, SamsungBrowser, Iron, Safari, Skyfire, Tizen, Webkit, PaleMoon, SailfishBrowser, Gecko, Trident, Presto, Goanna, iOS, Android, Sailfish, BlackBerry, BREW, Java, Windows Phone OS, Windows Phone, Windows CE, Windows NT, Symbian, webOS
Source:
Returns:
the version as float or NaN if User-Agent doesn't contain this version. Be careful when comparing this value with '==' operator!
Type
Number

versionStr(key) → {String}

Get the version (as String) of the given property in the User-Agent.
Parameters:
Name Type Description
key String a key defining a thing which has a version.
You can use one of following keys:

Mobile, Build, Version, VendorID, iPad, iPhone, iPod, Kindle, Chrome, Coast, Dolfin, Firefox, Fennec, Edge, IE, NetFront, NokiaBrowser, Opera, Opera Mini, Opera Mobi, UCBrowser, MQQBrowser, MicroMessenger, baiduboxapp, baidubrowser, SamsungBrowser, Iron, Safari, Skyfire, Tizen, Webkit, PaleMoon, SailfishBrowser, Gecko, Trident, Presto, Goanna, iOS, Android, Sailfish, BlackBerry, BREW, Java, Windows Phone OS, Windows Phone, Windows CE, Windows NT, Symbian, webOS
Source:
Returns:
the "raw" version as String or null if User-Agent doesn't contain this version.
Type
String