Mar*_*aio 47 javascript internet-explorer browser-detection conditional-comments
为了检测IE,大多数Javascript库都做了各种各样的技巧.
YAHOO.env.ua = function()(文件yahoo.js)中的用户代理进行正则表达式在阅读了这个答案之后,我想到了这是真的,为了在Javascript中简单地检测IE,我们可以简单地添加到我们的页面:
<!--[if IE]><script type="text/javascript">window['isIE'] = true;</script><![endif]-->
<script type="text/javascript" src="all-your-other-scripts-here.js"></script>
Run Code Online (Sandbox Code Playgroud)
现在,window.isIE为我们所有的Javascript代码设置变量,只需执行以下操作:
if(window.isIE)
...
Run Code Online (Sandbox Code Playgroud)
除了这可能导致痛苦,因为它必须在所有页面中添加之外,是否存在我可能不知道的任何问题/考虑因素?
仅供参考:我知道最好使用对象检测而不是浏览器检测,但有些情况下您仍然需要使用浏览器检测.
Mar*_*pel 59
James Padolsey 在GitHub上放了一个小片段,我在这里引用:
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
// And to detect the version:
// ie === 6 // IE6
// ie > 7 // IE8, IE9 ...
// ie < 9 // Anything less than IE9
// ----------------------------------------------------------
// UPDATE: Now using Live NodeList idea from @jdalton
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : undef;
}());
Run Code Online (Sandbox Code Playgroud)
当然所有的积分应该归詹姆斯,我只是信使(但如果我的复制粘贴动作错误,请拍摄信使).
还要看一下创建的叉子.Paul Irish在评论中解释了内部运作.
Alf*_*oML 40
如果你想这样做,我认为使用条件编译要好得多,因为你可以在javascript内部进行,而无需更改html:
var isIE = /*@cc_on!@*/false;
Run Code Online (Sandbox Code Playgroud)
Sea*_*mbo 26
Marcel Korpel的答案不再适用(在IE 10中它返回undef,因此IE 10看起来不是IE).注意:现在更新以使用IE 11.
这是该代码的变体,但它来自Microsoft的建议.如果您使用的是以前的代码,则可以直接使用此替换代码,因为它的构建方式与完全相同.
与条件注释/编译不同,它也应该与最小化器一起使用.
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
// And to detect the version:
// ie === 6 // IE6
// ie > 7 // IE8, IE9, IE10 ...
// ie < 9 // Anything less than IE9
// ----------------------------------------------------------
var ie = (function(){
var undef,rv = -1; // Return value assumes failure.
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
var trident = ua.indexOf('Trident/');
if (msie > 0) {
// IE 10 or older => return version number
rv = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
} else if (trident > 0) {
// IE 11 (or newer) => return version number
var rvNum = ua.indexOf('rv:');
rv = parseInt(ua.substring(rvNum + 3, ua.indexOf('.', rvNum)), 10);
}
return ((rv > -1) ? rv : undef);
}());
Run Code Online (Sandbox Code Playgroud)
更新以使用IE11.感谢'acarlon'指出它无法正常工作,并且'mario'代码我基于修复程序!
web*_*r55 11
IE 11已经发生了很大的变化,现在许多过去的浏览器检测方法都不起作用.以下代码适用于IE 11及更早版本.
function isIE()
{
var isIE11 = navigator.userAgent.indexOf(".NET CLR") > -1;
var isIE11orLess = isIE11 || navigator.appVersion.indexOf("MSIE") != -1;
return isIE11orLess;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我想我有你想要的东西.您可以使用Javascript和clientCaps将完整版本的Internet Explorer作为字符串"AA.BB.CCCC.DDDD".
http://www.pinlady.net/PluginDetect/IE/
它似乎适用于IE 5.5及更高版本(包括IE 10).它不受navigator.userAgent /文档模式/浏览器模式的影响.不需要条件注释或任何额外的HTML元素.这是一个纯粹的Javascript解决方案.
我现在不确定IE Mobile的行为方式,但是如果这个clientCaps方法失败,你总是可以使用备份检测方法.
到目前为止,我得说,它运作得很好.
| 归档时间: |
|
| 查看次数: |
70813 次 |
| 最近记录: |