10 Oct 2014

JavaScript判断IE版本

下面是我平时用到的和网上看到的一些区分浏览器版本及IE版本的代码:

<script>
/*@cc_on
@if (@_jscript_version > 5.7)
document.write("You are using IE8+");
@elif (@_jscript_version == 5.7 && window.XMLHttpRequest)
document.write("You are using IE7");
@elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest))
document.write("You are using IE6");
@elif (@_jscript_version == 5.5)
document.write("You are using IE5.5");
@else
document.write("You are using IE5 or older");
@end
@*/
</script>
<script>
// Option from Dean Edwards

//以下两行,利用IE支持条件编译进行判断
var ie = /*@cc_on!@*/false;

// Use the commented line:
var ie//@cc_on=1;

// Variation (shorter variable):

//以下两行,利用ie不支持垂直制表符判断
var ie = '\v'=='v';

// Option to Gareth Hayes (former record-holder):
var ie = !+"\v1";

//利用了IE与标准浏览器在处理数组的toString方法的差异做成的.
//对于标准游览器,如果数组里面最后一个字符为逗号,JS引擎会自动剔除它
var ie = !-[1,];
</script>

Tags:
0 comments