Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

识别特定浏览器最佳实践 #18

Closed
hotoo opened this issue May 29, 2013 · 9 comments
Closed

识别特定浏览器最佳实践 #18

hotoo opened this issue May 29, 2013 · 9 comments

Comments

@hotoo
Copy link
Owner

hotoo commented May 29, 2013

有时候在模块中需要针对某个特定浏览器进行识别,可以不需要、或不希望依赖 detector 进行识别(例如 sticky 只需要识别 IE6),这里提供一些最佳实践,供需要时参考。

基本原则

  1. 尽量针对特定功能使用正确的特性识别。
  2. 浏览器识别尽量使用 userAgent 信息。
  3. 不要使用不相干的特性检测。例如 isIE6 = !window.XMLHttpRequest;

最佳实践

针对 IE 或 IE 内核浏览器的代码兼容性支持:

// 单个浏览器识别:
var isIE6 = navigator.userAgent.indexOf("MSIE 6.0") !== -1;
var isIE7 = navigator.userAgent.indexOf("MSIE 7.0") !== -1;
var isIE8 = navigator.userAgent.indexOf("MSIE 8.0") !== -1;
var isIE9 = navigator.userAgent.indexOf("MSIE 9.0") !== -1;
var isIE10 = navigator.userAgent.indexOf("MSIE 10.0") !== -1;
//!var isIE11 = navigator.userAgent.indexOf("IE 11.0") !== -1;
var isIE11 = /\btrident\/[0-9].*rv[ :]11\.0/.test(navigator.userAgent);

// 多个浏览器识别
var isIE678 = /\bMSIE [678]\.0\b/.test(navigator.userAgent);

针对真实浏览器版本识别,可用于浏览器升级提示,但是为了避免将 IE 内核的浏览器识别为 IE,建议使用 detector:

function IEMode(){
  var ua = navigator.userAgent.toLowerCase();
  var re_trident = /\btrident\/([0-9.]+)/;
  var re_msie = /\b(?:msie |ie |trident\/[0-9].*rv[ :])([0-9.]+)/;
  var version;

  if(!re_msie.test(ua)){return false;}

  var m = re_trident.exec(ua);
  if(m){
    version = m[1].split(".");
    version[0] = parseInt(version[0], 10) + 4;
    version = version.join(".");
  }else{
    m = re_msie.exec(ua);
    version = m[1];
  }
  return parseFloat(version);
}


var ie = IEMode();
if(ie && ie < 8){
  // 你的浏览器太老了。
}

var isIE6 = navigator.userAgent.indexOf("MSIE 6.0") !== -1;
var isIE7 = IEMode() == 7;
var isIE8 = navigator.userAgent.indexOf("Trident 4.0") !== -1;
var isIE9 = navigator.userAgent.indexOf("Trident 5.0") !== -1;
var isIE10 = navigator.userAgent.indexOf("Trident 6.0") !== -1;
var isIE11 = navigator.userAgent.indexOf("Trident 7.0") !== -1;
@afc163
Copy link
Contributor

afc163 commented May 29, 2013

👍

@lianqin7
Copy link

这个东西好!好评哦亲

@lifesinger
Copy link

嗯,大道至简,UA 还是蛮好用的。

@hotoo
Copy link
Owner Author

hotoo commented May 29, 2013

不推荐,但是作为了解的内容 http://browserhacks.com/#ie

@hotoo hotoo closed this as completed May 31, 2013
lepture added a commit to lepture/gaussian-blur that referenced this issue Dec 20, 2013
@fredshare
Copy link

我看了detector,为什么一部分手机只能在node环境下检测?
image

@hotoo
Copy link
Owner Author

hotoo commented Dec 31, 2014

为什么这个问题要回复在这个 issue 里?

@fredshare
Copy link

哦,那应该回复到哪个issue

@afc163
Copy link
Contributor

afc163 commented Dec 31, 2014

为什么不新建

@Chuyue0
Copy link

Chuyue0 commented Feb 24, 2017

我想问,浏览器被更改了navigator数据后,得到的信息会变化吗?

Repository owner locked and limited conversation to collaborators Feb 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants