Book Image

Ext JS 3.0 Cookbook

Book Image

Ext JS 3.0 Cookbook

Overview of this book

Using Ext JS you can easily build desktop-style interfaces in your web applications. Over 400,000 developers are working smarter with Ext JS and yet most of them fail to exercise all of the features that this powerful JavaScript library has to offer. Get to grips with all of the features that you would expect with this quick and easy-to-follow Ext JS Cookbook. This book provides clear instructions for getting the most out of Ext JS with and offers many exercises to build impressive rich internet applications. This cookbook shows techniques and "patterns" for building particular interface styles and features in Ext JS. Pick what you want and move ahead. It teaches you how to use all of the Ext JS widgets and components smartly, through practical examples and exercises. Native and custom layouts, forms, grids, listviews, treeviews, charts, tab panels, menus, toolbars, and many more components are covered in a multitude of examples.The book also looks at best practices on data storage, application architecture, code organization, presenting recipes for improving themóour cookbook provides expert information for people working with Ext JS.
Table of Contents (15 chapters)
Ext JS 3.0 Cookbook
Credits
About the Author
About the Reviewer
Preface

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, and Ext.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, and Ext.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, and Ext.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 withExt.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.