Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110199
  • 博文数量: 19
  • 博客积分: 600
  • 博客等级: 上士
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-12 04:04
文章分类

全部博文(19)

文章存档

2014年(4)

2009年(2)

2008年(13)

我的朋友

分类:

2008-01-19 03:25:28

在 util.js 里用的.也可以简单的用 document.all 来判断浏览器类型.
 

DOM = (document.getElementById);
if (DOM) Detect = new BrowserDetector();
/**
 * Author: Chris Wetherell
 * BrowserDetector (object)
 *
 * A class for detecting version 5 browsers by the Javascript objects
 * they support and not their user agent strings (which can be
 * spoofed).
 *
 * Warning: Though slow to develop, browsers may begin to add
 * DOM support in later versions which might require changes to this
 * file.
 *
 * Warning: No one lives forever. Presumably.
 *
 * Typical usage:
 * Detect = new BrowserDetector();
 * if (Detect.IE()) //IE-only code...
 */

function BrowserDetector()
{
  //IE 4+

  this.IE = function()
  {
    try {
      return this.Run(document.all && !document.contains)!=false;
    } catch(e) {
      /* IE 5.01 doesn't support the 'contains' object and
         fails the first test */

      if (document.all) return true;
      return false;
    }
  }
  
  //IE 5.5+

  this.IE_5_5_newer = function()
  {
    try {
      return this.Run(this.IE() && Array.prototype.pop && !this.OPERA())!=false;
    } catch(e) {return false;}
  }
  
  //IE 5, Macintosh

  this.IE_5_Mac = function()
  {
      try {
        return (true == undefined);
      } catch(e) {
        return (
          document.all
          && document.getElementById
          && !document.mimeType
          && !this.OPERA()
        )!=false;
      }
  }
  //Opera 7+

  this.OPERA = function()
  {
    try {
      return this.Run(window.opera)!=false;
    } catch(e) {return false;}
  }
  //Gecko, actually Mozilla 1.2+

  this.MOZILLA = function()
  {
    try {
      return this.Run(
          document.implementation
          && document.implementation.createDocument
          && !document.contains
          && !this.OPERA()
          )!=false;
    } catch(e) {return false;}
  }
  //Safari

  this.SAFARI = function()
  {
    try {
      return this.Run(
          document.implementation
          && document.implementation.createDocument
          && document.contains
          )!=false;
      } catch(e) {return false;}
  }
  //Any browser which supports the W3C DOM

  this.DOM = function()
  {
    return (document.getElementById);
  }
  this.Run = function(test)
  {
    if (test==undefined) {
      return false;
    } else {
      return test;
    }
  }
  // This uses useragent for finer detection. If people spoof it, it's their

  // own fault when things break

  this.geckoVersion = function() {
    var matches = navigator.userAgent.match(/Gecko\/(\d*)/);
    if (matches && matches.length > 1) {
      return matches[1];
    }
    
    return null;
  }
}

阅读(4775) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~