Hide Table of Contents
What's New archive
Device Detection and Browser Sniffing

Device detection and browser sniffing are very useful when building mobile applications. The Dojo API dojo/has module can be used to inspect the browser and platform. See the Feature column in the Feature Names table for the dojo/has module.

Device Detection

The code snippet below can be used to detect if the device supports touch.

define(["dojo/has"], function(has){
 
// --------------------------------------------------------------------
 
// Environment is running on the browser platform
 
// check if the device supports touch
 
// touch-events
 
// --------------------------------------------------------------------
 
if (has("host-browser") && has("touch")) {
   
// touch specific code
 
}
 
else {
   
// non-touch specific code
 
}
});

Browser Sniffing

When building mobile applications you generally will target a smartphone, tablet, and the desktop. Targeting several different types of screens (devices) (including the desktop) will require support for several browsers. The Dojo API dojo/sniff module can also be used to detect the user's browser and version.

require(["dojo/sniff"], function(sniff) {
 
if(sniff("android") > 5 && sniff("chrome") > 44) {
   
// --------------------------------------------------------------------
   
// Android Platform:
   
// Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58K)
   
// AppleWebKit/537.36 (KHTML, like Gecko)
   
// Chrome/46.0.2490.76 Mobile Safari/537.36
   
// --------------------------------------------------------------------
    console
.log("Android Platform: %s", navigator.userAgent);
 
}
 
else if(sniff("ios") > 8 && sniff("safari") > 7) {
   
// --------------------------------------------------------------------
   
// iOS Platform: Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X)
   
// AppleWebKit/601.1.46 (KHTML, like Gecko)
   
// Version/9.0 Mobile/13B143 Safari/601.1
   
// --------------------------------------------------------------------
    console
.log("iOS Platform: %s", navigator.userAgent);
 
}
});

Additional information about dojo's has and sniff api can be found here.

Show Modal