Check out the latest documentation.

How to use Node.js API

Basic Usage

Include the following code in to your project .js file/files you want detection to take place in:

										
										var
										
									 Parser 
										
										=
										
									 require(
										
										'51degrees'
										
									).Parser; 

										
										var
										
									 psr 
										
										=
										
										
										new
										
									 Parser(
										
										'51Degrees-Lite.dat'
										
									); 

										
										var
										
									 ret 
										
										=
										
									 psr.parse(
										
										"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+"
										
									); 

console.log(ret); 

									

The above code will log the most basic information about requesting device to console. The requesting device in this case being a blackberry device. Of course simply outputting this information is not very useful. To integrate detection into your website logic each property can be accessed individually as shown below:

										
										if
										
									 (ret.IsMobile 
										
										==
										
										
										true
										
									) { 
 console.log(
										
										"-> MOBILE <-"
										
									); 
 
										
										//Device is mobile. Supply content for mobiles. 
										
									
} 
										
										else
										
									 { 
 console.log(
										
										"-> NOT MOBILE <-"
										
									); 
 
										
										//Device is not mobile. Supply content for desktops. 
										
									
} 

									

Specifying Properties

The above examples will always fetch a pre-defined list of properties. You can also instruct detector to fetch specific properties, or all Lite properties. The following example fetches all Lite properties:

										
										var
										
									 Parser 
										
										=
										
									 require(
										
										'51degrees'
										
									).Parser; 
properties 
										
										=
										
									 require(
										
										'51degrees'
										
									).ALL_PROPERTIES; 

										
										var
										
									 psr 
										
										=
										
										
										new
										
									 Parser(
										
										'./data/51Degrees.dat'
										
									, properties); 

										
										var
										
									 ret 
										
										=
										
									 psr.parse(
										
										"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+"
										
									); 

									

To manually specify desired properties you can supply an array of properties as a second parameter for the new Parser() line. For example:

										
										var
										
									 Parser 
										
										=
										
									 require(
										
										'51degrees'
										
									).Parser; 

										
										var
										
									 properties 
										
										=
										
									 [
										
										"DeviceType, IsMobile, ScreenPixelsWidth"
										
									]; 

										
										var
										
									 psr 
										
										=
										
										
										new
										
									 Parser(
										
										'51DL'
										
									, properties); 

										
										var
										
									 ret 
										
										=
										
									 psr.parse(
										
										"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+"
										
									); 

									

Please note that supplying properties that can only be found in a Premium or Enterprise data files will not return any result for the properties in question. I.e. using the above example with Lite data file will only return IsMobile and ScreenPixelsWidth properties but not the DeviceType property.