14

Blocking IE is definitely not best practice, but it's something in my requirements for an existing application. What's the most effective way to do that since conditional comments aren't available in IE 10? For IE 9 and below this will work:

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

Assuming there's a best practice JavaScript solution, what gotchas might I find? I'm wondering if there might be issues around the following:

  • Order of events firing
  • iframe elements that are beyond my control
  • Precedence of a JS solution in the context of other <script> tags
  • Scripts loaded via the document.write('<script type="text/javascript" src="foo.js"></script>'); method.

I have a feeling a lot of folks might be compelled to shout out "use Modernizr" and "Are you crazy, don't put scripts in the DOM that way!", unfortunately the application is large and some enhancements are outside the scope at this point.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
blong
  • 2,815
  • 8
  • 44
  • 110
  • 7
    Why on earth would you need to block IE10? – Pointy Feb 04 '13 at 15:45
  • 10
    When your client says so. – blong Feb 04 '13 at 15:48
  • 2
    http://www.quirksmode.org/js/detect.html – nbrooks Feb 04 '13 at 15:48
  • 9
    @b.long: I really wish you could tell clients they are wrong. :-P – gen_Eric Feb 04 '13 at 15:49
  • 3
    Is this something you can do serverside? – Mark Rushakoff Feb 04 '13 at 15:53
  • 2
    Damn! Mark Rushakoff beat me to it lol - but yes, serverside would make the most sense here for the requirements would it not? – Ben Duffin Feb 04 '13 at 15:54
  • @MarkRushakoff Even then someone could trick the server by sending another `user-agent` header. – 11684 Feb 04 '13 at 15:55
  • 2
    @11684 - true, but if they are manipulating the User-Agent pretty much anything you can do to detect the browser will be nulled ... but still, serverside would make the most sense for compatibility and speed – Ben Duffin Feb 04 '13 at 15:56
  • Yeah, truer (I know that's not a word) than my comment :D @BenDuffin – 11684 Feb 04 '13 at 15:57
  • @MarkRushakoff I'm ashamed to admit I hadn't _really_ considered a server-side solution. The problem is actually more complex, as I don't have original (non-compiled) source for **this** part of the application, but I suppose I may be able to hack something together in the index.gsp page. Right now, I'm liking [Shadow Wizard's](http://stackoverflow.com/a/14690499/320399) answer. – blong Feb 04 '13 at 16:03
  • Beware that what you are doing is wrong, and that Microsoft may actively try to make it fail. – SLaks Feb 04 '13 at 16:09
  • All, thanks so much for the input and helpful comments :) @SLaks your comment definitely gave me pause. Hopefully they'll continue supporting ` – blong Feb 04 '13 at 16:44
  • It is somewhat likely that they will drop support for VBScript _specifically to break this hack_. Don't do this! – SLaks Feb 04 '13 at 17:23
  • 2
    +1 for trying to block IE. In your case it's even an application requirement. Tell your designers that I love them. – Darin Dimitrov Feb 05 '13 at 21:50

3 Answers3

16

Well, IE is the only browser supporting client side VBScript.

So just add this to your pages: (except in IEblocked.html itself of course)

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

I know for a fact it's working in IE9 and below. This comment pretty much proves it's still working just fine in IE10 and as for the future I came across this blog post by Eric Lippert: Rumours of VBScript's Death Have Been Greatly Exaggerated which contains the following paragraph:

We will continue to support VBScript and JScript for the foreseeable future. Obviously VBScript, JScript, WSH, etc, must continue to be shipped with the operating system forever, as huge amounts of existing business-critical code depends upon them. To characterize that as "dying a slow death" is excessively melodramatic. We expect that the unmanaged COM scripting languages will continue to be useful for many, many years. The Visual Studio Sustaining Engineering Team presently is responsible for VBScript, JScript, Windows Script Components, Windows Script Host, etc.

Although posted over 8 years ago, I strongly belive we still have long years of VBScript existence in the core of Windows, and future versions of Internet Explorer will keep using it.

To sum things up, I have contacted Eric directly and asked "How long will VBScript will be supported, as client side language, in Internet Explorer versions?". In response, he said:

There are still many corporate clients who depend on VBScript in the browser, so MSFT would be foolish to drop support for it. It is very cheap to support, and losing good will of even a small number of customers is expensive.

He's no longer working in Microsoft so his answer is not official but it's the closest I can get and not years old blog post but directly from the source. All things considered, I can conclude that using the above code is going to work for many more years to come. :)

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • 1
    Now thats a nice idea! Never thought of using vbScript to specifically target Exploders .... another tool for the toolbox! – Ben Duffin Feb 04 '13 at 16:00
  • 7
    This is deliciously evil. +1. – vcsjones Feb 04 '13 at 16:01
  • 1
    just checked IE10 and it does recognize VBScript – hubson bropa Feb 04 '13 at 16:11
  • 1
    I wish I could upvote this answer again! Thanks for expanding on the life-expectancy of VBScript. There's [a question on SO about it](http://stackoverflow.com/q/4968712/320399), although it doesn't seem to have an authoritative answer - I still appreciate anything on StackOverflow compared to social.msdn.microsoft.com – blong Feb 05 '13 at 14:36
  • 2
    Cheers, I tried to contact Eric Lippert directly still waiting for a reply. :) – Shadow The GPT Wizard Feb 05 '13 at 14:41
5

Your client is nuts. But if they're paying to do this, then.... meh, whatever.

If you're using jQuery, it provides a browser detection feature. It is deprecated, so you may want to avoid using the latest versions, but it does work:

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

If you're not using jQuery, or if you prefer not to use deprecated features, you can do it by:

  • Parsing the User Agent string:

    if(!!navigator.userAgent.match(/MSIE/)) { .... do crazy stuff .... }
    
  • Using Javascript conditional comments (which I believe are still supported):

    if(Function('/*@cc_on return document.documentMode > 0@*/')()){ .... do crazy stuff .... }
    

Hope that helps.

SDC
  • 14,192
  • 2
  • 35
  • 48
0

for future reference here is all the detections for ie in 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
        }
    }
}

Victoria French
  • 756
  • 5
  • 10
  • 1
    **prove it wrong**. The only two major browsers using document.all are Opera and IE. And while I don't condone using this code for javascript, there are times when it is needed to create clear class extensions for CSS use. Things that are not detectable such as gradient backgrounds with border-radius in IE.. where its one way in 7, 8 and 9 but if you don't detect 9 then it will read 8's method and foobar you. But hey I gave an answer which is more than you did. – Victoria French Feb 11 '13 at 17:18