显然 IE11 的改进破坏了几乎所有用于区分 W3C 和 IE 浏览器的方法,使得无数现存代码无法取得其是 IE 浏览器的事实。或许这就是微软的目的:IE11 不再是“IE”了,你不需要在判断出来它了。
下面给出使用 UA 获取 IE 版本的代码:
/** * Get version if it‘s a microsoft internet explorer. * * @return version code,or else null if it‘s not a IE. */ function getIEVersion(){ var ua = navigator.userAgent,matches,tridentMap={‘4‘:8,‘5‘:9,‘6‘:10,‘7‘:11}; matches = ua.match(/MSIE (\d+)/i); if(matches&&matches[1]) { //find by msie return +matches[1]; } matches = ua.match(/Trident\/(\d+)/i); if(matches&&matches[1]) { //find by trident return tridentMap[matches[1]]||null; } //we did what we could return null; }
由于 UA 可以随意伪造,所以并没有合适的方法能够保证检测出真正的浏览器环境,因此代码都不应依赖于具体的环境和版本。