如何使用条件编译来嗅探IE8
我想嗅探当前版本的IE8(我很抱歉,但没有其他办法,我必须这样做)。我知道应该使用功能嗅探而不是浏览器嗅探,但是嘿,我的老板不知道,对吧?
现在我使用了这个条件编译代码:
var ie8 = false/*@cc_on @_jscript_version == 8@*/
但似乎它还包括IE7。有什么想法吗?
I want to sniff the current version of IE8 (and I'm so sorry, but no other way, I have to do that). I know that feature sniffing should be used instead of browser sniffing, but hey, my boss doesn't know that, right?
Now I used this conditional compiling code:
var ie8 = false/*@cc_on @_jscript_version == 8@*/
But seems that it also includes IE7. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在不使用条件编译的情况下,您可以使用条件注释在您想要测试的每个 IE 的
html
元素上设置一个类,例如:(来自 http://initializr.com/)
那么在 JS 中测试该类就很简单了:
Without using conditional compiles, you could use conditional comments to set a class on the
html
element for each IE you want to test for, like:(from http://initializr.com/)
Then it would be a simple matter of testing for the class in JS:
以下假设您使用的是 Internet Explorer...
首先,测试浏览器版本极其异常 - document.documentMode 是应该测试的(因为页面可以处于 IE8 模式)即使使用 IE9 或 IE10):
如果您有一些非常奇怪的要求,那么您可以可靠地检测 JScript 版本,例如 IE8 的 _jscript_version 为 5.8(请参阅http://en.wikipedia.org/wiki/Conditional_comment ):
条件注释放在字符串中这样它就不会被可能发生的任何 JavaScript 压缩删除(这可能会删除注释)。
重要的 Internet Explorer 11 编辑:
The following presumes you are using Internet Explorer...
Firstly, it is extremely abnormal to test for the browser version - the document.documentMode is what should be tested for (because a page can be in IE8 mode even if using IE9 or IE10):
If you have some really strange requirement, then you can reliably detect the JScript version e.g. _jscript_version is 5.8 for IE8 (see http://en.wikipedia.org/wiki/Conditional_comment ):
The conditional comment is put inside a string so that it won't be removed by any JavaScript compression that might occur (which may strip out comments).
Important Internet Explorer 11 edit:
为什么不使用条件注释来切换设置变量的脚本?喜欢:
虽然它有点冗长,但也许它也更可靠。
Why not use conditional comments to toggle a script that sets the variable? Like:
Granted it's a bit verbose, but perhaps it's also more reliable.