只嗅IE8

Zo7*_*o72 3 javascript detection internet-explorer-8

如果浏览器是IE8,那么最短的javascript代码是什么?

我想为IE8做一些特别的事情

我不想连接一个能够让我回到所有浏览器和版本的库.如果它是IE8,我只想写一两行来解决问题.

谢谢你

Sae*_*ati 10

我发现的最好方法是使用条件样式或条件编译:

var isIE8 = false;
<!--[if IE 8]>
        <script type='text/javascript'>
            isIE8 = true;
        </script>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

然后在您的代码中,您只需检查此值:

<script type='text/javascript'>
   if (isIE8) {
       alert('Your browser is IE8');
   }
</script>
Run Code Online (Sandbox Code Playgroud)


Ren*_*Pot 5

您可以使用以下方法在HTML中执行此操作:

<!--[if IE 8]>
<script type="text/javascript" src="IE8.js">
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

另请参阅本教程:http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx