new MobileDetect(userAgent)
Constructor for MobileDetect object.
Such an object will keep a reference to the given user-agent string and cache most of the detect queries.
Such an object will keep a reference to the given user-agent string and cache most of the detect queries.
Parameters:
Name | Type | Description |
---|---|---|
userAgent |
string | typically taken from window.navigator.userAgent or http_header['User-Agent'] |
- Source:
Example
var md = new MobileDetect(window.navigator.userAgent);
if (md.mobile()) {
location.href = (md.mobileGrade() === 'A') ? '/mobile/' : '/lynx/';
}
Methods
-
is(key) → {boolean}
-
Global test key against userAgent, os, phone, tablet.
Parameters:
Name Type Description key
String - Source:
Returns:
- Type
- boolean
-
match(pattern) → {boolean}
-
Do a quick test against navigator::userAgent.
Parameters:
Name Type Description pattern
String | RegExp (a string will be converted to a case-insensitive RegExp. - Source:
Returns:
- Type
- boolean
-
mobile() → {String}
-
Returns the detected phone or tablet type or null if it is not a mobile device.
Shortcut for md.phone() || md.tablet()- Source:
Returns:
- Type
- String
-
mobileGrade() → {string}
-
Returns the mobile grade ('A', 'B', 'C').
- Source:
Returns:
- Type
- string
-
os() → {String}
-
Returns the detected operating system string or null.
- Source:
Returns:
- Type
- String
-
phone() → {String}
-
Returns the detected phone type/family string or null.
- Source:
Returns:
- Type
- String
-
tablet() → {String}
-
Returns the detected tablet type/family string or null.
- Source:
Returns:
- Type
- String
-
userAgent() → {String}
-
Returns the detected user-agent string or null.
- Source:
Returns:
- Type
- String
-
version(testKey) → {Number}
-
Check the version 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 testKey
String - Source:
Returns:
the version as float- Type
- Number