2013-02-04 111 views
14

阻止IE绝对不是最佳做法,但这是我对现有应用程序的要求。从IE 10中的conditional comments aren't available开始,最有效的方法是什么?对于IE 9和下面这将工作:阻止现代IE浏览器?

<!--[if IE]> 
<script type="text/javascript"> 
window.location = "/IEblocked.html"; 
</script> 
<![endif]--> 

假设有JavaScript解决方案,什么陷阱我可能会找到一个最佳做法?我不知道是否有可能是问题围绕以下内容:事件

  • 订购射击
  • iframe元素是我无法控制的其他<script>标签
  • 的背景下,JS解决方案的
  • 优先
  • 通过document.write('<script type="text/javascript" src="foo.js"></script>');方法加载的脚本。

我有一种感觉很多人可能会被迫喊出“使用Modernizr的”“你疯了,不要把脚本的DOM这样!”,不幸的是这个应用程序很大,并且一些增强功能在这个范围之外。

+7

为什么地球上,你会需要阻止IE10的检测? – Pointy

+9

当你的客户这么说。 – blong

+2

http://www.quirksmode.org/js/detect.html – nbrooks

回答

15

那么,IE是唯一支持客户端VBScript的浏览器。

所以才添加到您的网页:(除IEblocked.html本身当然)

<script type="text/vbscript"> 
Document.Location = "IEblocked.html" 
</script> 

我知道它的工作在IE9和下面的事实。 This comment几乎证明它仍然在IE10工作得很好,并为未来的我碰到这个博客帖子走过来埃里克利珀:Rumours of VBScript's Death Have Been Greatly Exaggerated其中包含以下段落:

我们将继续支持VBScript和JScript的可预见的未来。很显然,VBScript,JScript,WSH等必须继续随操作系统一起交付,因为大量现有的业务关键代码依赖于它们。将其描述为“死于缓慢的死亡”是过于戏剧化的。我们期望多年来,非托管COM脚本语言将继续有用。在Visual Studio可持续工程团队目前负责的VBScript,JScript中,Windows脚本组件,Windows脚本宿主等

虽然在8年前发布的,我强烈相信的时候,我们仍然有漫长的岁月VBScript中存在的Windows的核心,未来版本的Internet Explorer将继续使用它。

总结一下,我直接联系了Eric,并问“在Internet Explorer版本中,VBScript将作为客户端语言支持多久?”。作为回应,他说:

仍然有许多公司客户在浏览器中依赖VBScript,因此MSFT将愚蠢地放弃对它的支持。支持起来非常便宜,即使是少数客户的良好意愿也很昂贵。

他不再在微软工作,所以他的答案不是官方的,但它是最接近我可以得到的,而不是几年的博客文章,但直接从源头。所有事情都考虑到了,我可以得出结论,使用上述代码将在未来的许多年内发挥作用。 :)

+1

现在这是个好主意!从来没有想过使用VBScript专门针对爆炸物......另一个工具箱的工具! –

+7

这是美味的邪恶。 +1。 – vcsjones

+1

刚刚检查IE10,它确实识别VBScript –

4

你的客户是坚果。但如果他们付钱去做这件事,那么......呃,不管。

如果您使用jQuery,它提供了浏览器检测功能。它已经过时,所以你可能要避免使用最新版本,但它的工作:

if ($.browser.msie) { ....do crazy stuff here.... } 

如果你不使用jQuery,或者如果你不喜欢使用已过时的功能,你可以做到这一点:

  • 解析用户代理字符串:

    if(!!navigator.userAgent.match(/MSIE/)) { .... do crazy stuff .... } 
    
  • 使用JavaScript有条件的意见(我相信仍然支持):

    if(Function('/*@cc_on return document.documentMode > [email protected]*/')()){ .... do crazy stuff .... } 
    

希望有帮助。

+1

这就是所谓的条件编译。 –

+0

'.match(/ MSIE /)'是愚蠢的天真。 – Raynos

0

这里将来参考是所有即在javascript

<script type="text/javascript"> 

// if I am opera I need to not enter this function 
if (!!(window.opera && window.opera.version)) { 
    // ok now am I IE (opera is the only other browser that will do this 
    if (document.all) { 
     // Now lets look at the versions, use the ones you want 

     // ie 5 
     if (!(document.compatMode != undefined)) { 
      // do ie 5 thing 
     } 
     else if ((document.compatMode != undefined) && (window.XMLHttpRequest == undefined)) { 
      // do ie 6 thing 
     } 
     else if ((document.XMLHttpRequest != undefined) && (document.querySelector == undefined)) { 
      // do ie 7 thing 
     } 
     else if ((document.querySelector != undefined) && (document.addEventListener == undefined)) { 
      // do ie 8 thing 
     } 
     else if ((document.addEventListener != undefined) && (window.atob == undefined)) { 
      // do ie 9 thing 
     } 
     else if (window.atob) { 
      // do ie 10+ thing 
     } 
     else { 
      // do ie 4 thing 
     } 
    } 
} 

+1

lolwut。 'if(document.all){/ *然后IE * /}'傻。 – Raynos

+1

**证明它错了**。 Opera和IE只有两个使用document.all的主要浏览器。虽然我不会宽恕使用JavaScript代码,但有时需要为CSS使用创建清晰的类扩展。事情是不可检测的,如IE中的边界半径渐变背景......其中7,8和9中的一种方式,但如果你没有检测到9,那么它将读取8的方法并foobar你。但是,嘿,我给出了比你更多的答案。 –