DIV CSS 佈局教程網

js識別不同浏覽器
編輯:JavaScript綜合知識     

   檢測浏覽器,注意浏覽器判斷順序,主要是基於userAgent做判斷。

  //檢測浏覽器

  var client = function(){

  var engine = {

  ie:0,

  gecko:0,

  webkit:0,

  khtml:0,

  opera:0,

  ver:null

  };

  var browser = {

  //浏覽器

  ie: 0,

  firefox: 0,

  safari: 0,

  konq: 0,

  opera: 0,

  chrome: 0,

  ver:null

  };

  var ua = navigator.userAgent;

  //浏覽器檢測有順序

  if(window.opera){//opera偽裝,所以優先檢測

  engine.ver = browser.ver = window.opera.version();

  engine.opera = browser.opera = parseFloat(engine.ver);

  } else if(/AppleWebKit/(S+)/.test(ua)){

  engine.ver = RegExp["$1"];

  engine.webkit = parseFloat(engine.ver);

  //確定是 Chrome 還是 Safari

  if (/Chrome/(S+)/.test(ua)){

  browser.ver = RegExp["$1"];

  browser.chrome = parseFloat(browser.ver);

  } else if (/Version/(S+)/.test(ua)){

  browser.ver = RegExp["$1"];

  browser.safari = parseFloat(browser.ver);

  } else {

  //近似地確定版本號

  var safariVersion = 1;

  if (engine.webkit < 100){

  safariVersion = 1;

  } else if (engine.webkit < 312){

  safariVersion = 1.2;

  } else if (engine.webkit < 412){

  safariVersion = 1.3;

  } else {

  safariVersion = 2;

  }

  browser.safari = browser.ver = safariVersion;

  }

  }else if (/KHTML/(S+)/.test(ua) || /Konqueror/([^;]+)/.test(ua)){

  engine.ver = browser.ver = RegExp["$1"];

  engine.khtml = browser.konq = parseFloat(engine.ver);

  }else if (/rv:([^)]+)) Gecko/d{8}/.test(ua)){

  engine.ver = RegExp["$1"];

  engine.gecko = parseFloat(engine.ver);

  //確定是不是 Firefox

  if (/Firefox/(S+)/.test(ua)){

  browser.ver = RegExp["$1"];

  browser.firefox = parseFloat(browser.ver);

  }

  }else if (/MSIE ([^;]+)/.test(ua)){

  engine.ver = browser.ver = RegExp["$1"];

  engine.ie = browser.ie = parseFloat(engine.ver);

  }

  return {

  engine:engine,

  browser: browser

  };

  }();

  //調用

  if (client.engine.webkit) { //if it’s WebKit

  if (client.browser.chrome){

  //執行針對 Chrome 的代碼

  } else if (client.browser.safari){

  //執行針對 Safari 的代碼

  }

  } else if (client.engine.gecko){

  if (client.browser.firefox){

  //執行針對 Firefox 的代碼

  } else {

  //執行針對其他 Gecko 浏覽器的代碼

  }

  }

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved