javascript 我如何只为 IE 8 运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18067999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do I run a script only for IE 8
提问by R Doolabh
I have a js file and would like to run some code only if the browser is IE 8. Is there a way I can do this?
我有一个 js 文件,并且只想在浏览器是 IE 8 时运行一些代码。有没有办法做到这一点?
Right now I only have this but it's for all ie browsers:
现在我只有这个,但它适用于所有 ie 浏览器:
if ($.browser.msie) {
//do stuff for IE 8
}
回答by Hamza Kubba
See Browser detection in JavaScript?and http://modernizr.com
请参阅JavaScript 中的浏览器检测?和http://modernizr.com
Here is the short(est?) answer:
这是简短的(est?)答案:
if (navigator.userAgent.match(/MSIE 8/) !== null) {
// do something in IE8
}
回答by seanmk
Browser specific code is almost never a good idea. However, if you must do this, you can put your code inside conditional comments.
浏览器特定的代码几乎从来都不是一个好主意。但是,如果您必须这样做,您可以将您的代码放在条件注释中。
http://www.positioniseverything.net/articles/cc-plus.html
http://www.positioniseverything.net/articles/cc-plus.html
<!--[if IE 8]>
<script type="text/javascript">
// this is only executed for IE 8
</script>
<![endif]-->
回答by R Doolabh
Alright people, I solved it myself like this:
好吧,我自己解决了这样的问题:
if ($.browser.msie && $.browser.version == 8) {
//my stuff
}
回答by anarchocurious
You can do something like this:
你可以这样做:
if (navigator.userAgent.indexOf('MSIE 8.0') !== -1) {
// this clause will only execute on IE8
}
回答by Musa
$.browser is deprecated so you can not use this object property in new jQuery version so to support this you have to use the jQuery migrate plugin.Here is the link. https://github.com/jquery/jquery-migrate/#readme
$.browser 已弃用,因此您不能在新的 jQuery 版本中使用此对象属性,因此要支持此功能,您必须使用 jQuery 迁移插件。这是链接。 https://github.com/jquery/jquery-migrate/#readme
回答by Nathan
Could you do a call in the header to redirect the page if it is less than IE9? I find myself doing this often for my clients that use a mixture of IE6-10.
如果页面小于 IE9,您可以在标题中调用以重定向页面吗?我发现自己经常为混合使用 IE6-10 的客户执行此操作。
In the document head I call,
在我调用的文件头中,
<!--[if IE 7]> <link rel="()" type="()" href="(Your link here.)"><![endif]-->
The other option you could look into is a shim or polyfil. There are many on the internet that help bridge the gap between modern web design and older browsers. An example is Modernizr.
您可以研究的另一个选项是垫片或 polyfil。互联网上有很多帮助弥合现代网页设计和旧浏览器之间的差距。一个例子是Modernizr。
回答by mirelon
You can do it with conditional comments (by https://stackoverflow.com/a/10965091/1878731):
您可以使用条件注释(来自https://stackoverflow.com/a/10965091/1878731):
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
if ($('html').is('.ie8')) {
...
}
Or without messing with html (by http://en.wikipedia.org/wiki/Conditional_comment):
或者不弄乱 html(通过http://en.wikipedia.org/wiki/Conditional_comment):
<script>
/*@cc_on
@if (@_jscript_version == 5.8) { // IE8 detected
// do something
}
@*/
</script>
回答by Deepak Kumar
Try using like this
尝试像这样使用
<script type="text/javascript">
if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
document.write('<script src="somescript.js"><\/script>');
</script>
回答by Sanu Uthaiah Bollera
You can use the document.documentModeto get the version of IE.
您可以使用document.documentMode来获取 IE 的版本。
switch(document.documentMode)
{
case 8:
//do something with IE8
break;
case 9:
//do something with IE9
break;
case 10:
//do something with IE10
break;
case 11:
//do something with IE11
break;
default:
console.log('Other browsers...');
}
For documentation refer here
有关文档,请参阅此处