Detecting browsers and platforms used by clients
Although Ext JS is a cross-browser library, there are instances when your application needs to show a different behavior depending on the user's browser or platform. Browser and platform detection are very simple tasks with Ext JS, and this recipe shows how it's done.
How to do it...
You can detect various browsers and platforms used by your clients in the following way:
You can use
Ext.isChrome
to find out if the detected browser is Chrome:var browser = ""; if (Ext.isChrome) { browser = "Hi! I'm the new kid on the block"; }
The browsers such as Mozilla and Firefox that use the Gecko rendering engine are detected with
Ext.isGecko, Ext.isGecko2
, andExt.isGecko3:
if (Ext.isGecko) { browser = "Gecko"; } if (Ext.isGecko2) { browser = "Gecko2"; } if (Ext.isGecko3) { browser = "We like Firefox!"; }
The Internet Explorer versions are flagged by
Ext.isIE, Ext.isIE6, Ext.isIE7
, andExt.isIE8:
if (Ext.isIE) { browser = "IE"; } if (Ext.isIE6) { browser = "Get a decent browser, now!"; } if (Ext.isIE7) { browser = "IE7"; } if (Ext.isIE8) { browser = "IE8"; }
Opera is detected with
Ext.isOpera:
if (Ext.isOpera) { browser = "Opera"; }
And finally, Safari is detected with
Ext.isSafari, Ext.isSafari2, Ext.isSafari3
, andExt.isSafari4:
if (Ext.isSafari) { browser = "Safari"; } if (Ext.isSafari2) { browser = "Safari2"; } if (Ext.isSafari3) { browser = "Safari3"; } if (Ext.isSafari4) { browser = "Safari4"; }
When it comes to platform detection, Adobe's Air is detected with
Ext.isAir:
var platform = ""; if (Ext.isAir) { platform = "Air"; }
Linux is detected with
Ext.isLinux:
if (Ext.isLinux) { platform = "Linux"; }
Mac OS is detected with
Ext.isMac:
if (Ext.isMac) { platform = "Mac"; }
Windows is detected with
Ext.isWindows:
if (Ext.isWindows) { platform = "Windows "; }
How it works...
As you can imagine, the values for Ext JS's browser and platform type flags are all obtained from parsing the value of the userAgent
property of the JavaScript navigator object.
There's more...
Use this recipe when you need to alter the features or behavior of your application depending on the browser or platform being used. For example, depending on the platform used, you can provide Mac or Windows versions of a browser plugin; or you can account for rendering differences of various browsers when positioning DOM elements.