官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 洪江市| 隆德县| 乡城县| 常德市| 明水县| 滦南县| 阳曲县| 雷波县| 台安县| 锦屏县| 沾化县| 柳林县| 景东| 岑溪市| 清河县| 北安市| 牡丹江市| 施甸县| 凤城市| 汝城县| 铁岭县| 古蔺县| 剑河县| 平定县| 乐亭县| 岗巴县| 西昌市| 高平市| 南澳县| 正安县| 无棣县| 满洲里市| 广灵县| 天门市| 宜兰市| 大冶市| 长丰县| 和田市| 新闻| 阿巴嘎旗| 土默特右旗|