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

  • Ext JS 3.0 Cookbook
  • Jorge Ramon
  • 389字
  • 2021-04-01 13:43:43

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.

主站蜘蛛池模板: 和田市| 阿尔山市| 穆棱市| 邛崃市| 苍梧县| 温泉县| 辽阳县| 阿坝| 澄城县| 肇庆市| 海安县| 定远县| 涿鹿县| 奎屯市| 太和县| 德兴市| 武穴市| 郴州市| 辽中县| 贺兰县| 黑龙江省| 东城区| 秦皇岛市| 清流县| 汨罗市| 富蕴县| 伊川县| 汕头市| 额敏县| 泸水县| 济源市| 泊头市| 江永县| 合山市| 云南省| 南投县| 石家庄市| 怀来县| 新竹市| 石泉县| 德阳市|