Detect & Redirect Script For Internet Explorer (IE)

NOTE: For anyone looking for the ability to detect and redirect based on IE version, you should check out this post. It uses conditional comments to detect IE versions and redirect accordingly.

dieie.jpgI hate Internet Explorer.

There, I’ve said it. Perhaps it will help you to understand why I sat down tonight and decided to ex-out the worst of the bunch, Internet Explorer 5.5 (and below).

It pains me, physically pains me, to open up Internet Explorer after having designed something using Firefox as my primary browser. Granted, with IE7, things have improved and my design often (in a very general way) resembles what it should. However, taking steps backwards in the IE pedigree, things quickly go from worse to craptastic.

So, I decided, this is my site, and I should be able to set the standards for what “type” of people I want on here (you all, of course, are welcome anytime). I went looking for a script to help me do it, and couldn’t find one (to be honest, I didn’t look very hard). I found several that would pick out your browser type, and version. But the problem with these is the version number generally only goes as high as 4 – which all IE5+ browsers are.

My parameters were simple. If the person visiting the site had any browser other than Internet Explorer – they could come in. And to show I’m not a complete brute, I decided that those visiting with IE7, and IE6 could also come and play. Otherwise you get redirected to a page that informs the visitor that they might want to reconsider their choice of Web surfing app.

If you just want the files to make this happen, just right-click and select Save Link As…:

ifoldie.js oldie.html warning.jpg

Otherwise, lets put this together step by step.

Building the Script

First we need to open a blank text file (notepad or any plain text editor will do) to save as a Javascript file. Then, assign your variables:

<!--
var browser = navigator.appName
var ver = navigator.appVersion
var thestart = parseFloat(ver.indexOf("MSIE"))+1
var brow_ver = parseFloat(ver.substring(thestart+4,thestart+7))

The variables do the following:

  1. browser: Gets the browser type (IE, Netscape, etc.).
  2. ver: Gets the version string. A long string of info with various bits of goo in it.
  3. the start: We need this number for the next part – trust me.
  4. brow_ver: This is the IE version number (generally 5.5, 6.0 or 7.0), converted from a string into an integer.

Next, we need to create an IF conditional that decides whether the browser type is Internet Explore, and checks if the IE version is less than 6**. And if it is, we need to tell the browser to redirect to a pre-created HTML file instead of displaying the site.

if ((browser=="Microsoft Internet Explorer") && (brow_ver < 6))
{
window.location="http://www.yourdomain.com/redirectfile.html";
}
//-->

Implementing it

Then, simply make sure you have the redirectfile.html (or whatever page you wish to redirect the misguided and outdated user to) created. Save the text file as something.js (I like the name ifoldie.js, but that’s just me), and add the following line in between the <head></head> tags of your Web page:

<script type="text/javascript" src="http://www.yourdomain.com/ifoldie.js">
</script>

And there you have it.

As you can see from my redirect page, I’ve also added the user a convenient way to upgrade to a better browser.

** This can be changed to either be more diligent or less on who it allows access. If you want to redirect all users of IE6 and below, simply change this to 7. Likewise, it can be changed to 5.5 or 5 (or any lower number) to allow a greater range of access.

Add comments for any questions or suggestions.

79 Comments to “Detect & Redirect Script For Internet Explorer (IE)

  1. Levi says:

    awesome, thanks!

  2. If I want to re-direct a user with 6 or lower i assume i just need to change if conditional to… brow_ver

  3. Ryan says:

    Hey Jerry, yes, just change the “6″ to a “7″ and this will only allow IE browsers of version 7 or higher (should they decide to continue making the piece of junk) through.

  4. Ryan,
    Thanks great script!
    What if I wanted to add Firefox to this script with a redirect to a different page?
    Do i need to add another variable? and if so what would (ver.indexOf(“***”))+1 look like? and I’m assuming the if conditional would just need to be added with the ((browser=”Firefox”)&&(brow_ver

  5. Ryan says:

    Hey Jerry, you could modify it to redirect for Firefox. The browser type will be listed as “Netscape” though, as Firefox/Mozilla is a branch of the original Netscape.

    The tricky part is that this script cuts up the rather long Browser Version string to find out which actual version of IE you’re using. Check out this page. It has a script on it that displays your browser information.

    If you’re using Firefox (as I am), you’ll see a string similar to:
    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6

    I don’t have the time to work out the math on it, but you just need to adjust the code in my script to cut that string up to give what Firefox version you’re using.

    If you want to redirect ALL Firefox browsers, that’s quite easy. Just search the string for “Firefox” and if it returns true, redirect.

  6. erik says:

    since about 85% of the world uses ie, its best to have two websites, one compatable with ie and one with firefox, I know it sucks, but its the most pro way.

  7. Ryan says:

    Hey Erik, two Web sites isn’t really necessary – using IE’s conditional comments, adding alternative style sheets is a much better way to go about it. Just setting up secondary CSS rules to adjust for IE’s retardedness.

    However – I still like the script, as it’s my little way of informing the world that they’re using a stupid browser. :-)

  8. Snadowitz says:

    Just what I have been looking for all these weeks – the dogs-danglies – simple and it works!!!

  9. Valehru says:

    Ryan, the idea seems very silly to me. I like browsing the web with lynx (text based browser), what good is it for me to use firefox? Don’t force people to choose, just code to the right standards, prove that your site validates to wC3 standards and then let the browsers update to the international standards. For beilabs.com I have only one stylesheet, and zero hacks for IE, Safari or Firefox and it renders perfectly in all of them.

    Let the users choose whatever browser they want on their own! If people want a crap web experience then that is their own choice.

  10. Ryan says:

    Hey Valehru, didn’t know you headed back to Ireland.

    I’m not forcing anyone to choose. You browsing in a text browser, someone browsing in Safari or Opera, or whatnot, are not going to notice anything different.

    The only people affected on this site are people using an old outdated browser (IE5.5 or less) – which happens to break your site as well btw.

    The problem with your solution is a single style sheet with no alternate stylesheet (or if absolutely needed, hacks) is not possible in a more complex design with lots of elements.

    For a personal site (such as this, or any of my other sites) I can choose to do things like this redirect, however for clients sites, it most definitely will require that it look good in IE and all the best coding to standards in the world isn’t going to solve certain problems with IE6 – still the most widely used browser.

    Not to look up your skirt, but you’re using an IE hack in your CSS for this very reason.

    I do agree 100% that users should be allowed to choose whatever browser they want – but browser creators need to start more strictly adhering to standards. Once that happens (and it could be only a year or two away), life for your average CSS designer, will be a helluva lot easier.

  11. Valehru says:

    Whoops, I had forgotten about that one. Must work on that CSS page a little more to get full compliance. Dammit. :D

    Browser creators, looking at MS in particular, are never going to comply completely with all these standards. They way they see it is that they have the market share and that’s that.

    Personally I despise IE with a passion, unfortunately in my line of work IE development is required. I once had a spec from a US client who demanded complete compliance from IE 5 and IE 6. Quite a difficult task. Fortunately with machines designed with IE 5 as a core browser are now reaching the end of their life cycle and are being phased out.

  12. Ryan says:

    Exactly. I’m definitely not suggesting that people use this on the site’s of clients/businesses/etc. But for me, as I do both my own and “for hire” design, I just wanted this little script for my sites as they aren’t focused on business, and allow me to kindly kick people in the pants should they be using an out-dated browser.

    And in regards to compliance, you gotta hand it to the swell folks on the Firefox development crew, as according to the figures on that link I gave in my last comment, they’re edging out IE one dissatisfied user at a time – and they do a reasonable job of maintaining compliance.

  13. Ryan says:

    DOES WORK!!! :-) DON’T YELL!!!

  14. Darren says:

    I’m not sure this works, I’m trying to block IE6 and below browsers because I refuse to stoop to the level of using hacks and filters. More people are using IE7 and at least IE7 follows CSS standards better than it ever has.
    I tested this script on a windows 2000 platform with IE6 and it didn’t redirect me to the page at all.

  15. Ryan says:

    Hey Darren, I feel your pain man. I’ve not tested this using W2K, but if you copy and pasted the code above, I just noticed that in the first section of code – setting up the variables – the quotes are fancy quotes, and that will screw stuff up. Double check that.

    An easy way to see if it’s working is to use http://ipinfo.info/netrenderer/index.php. Set it to IE 5.5 or less and put in daobydesign.com and you’ll see how it’s intended to work.

  16. Darren says:

    Ryan,

    That’s EXACTLY it- it works! I didn’t even notice I had fancy quotes in the code, I’m glad you caught that.

    Let me say that the code OFFICIALLY works for IE6 version browsers and below.

    I recently learned CSS and found it to be a true pain in the @$$ that in order to make a site look good on IE6 – you have to use all sorts of hacks and bug fixes. I’m with you Ryan. CSS is a standard and browsers should try following those standards or be rejected. I’m sure there will be people who disagree because 36% still use IE6 but it sure would be nice to see everybody up to speed on web standard compliant browsers. This way developers can focus on creating great web sites without having to pull out their hair trying to find the right hack, bug fix, or filter.

  17. wowfood says:

    ahha this is damend useful for me. I’m working on a site for a uni course. Complys fully with xhtml(strick)1.0 and fully with CSS3.0 (doesn’t comply with 2.0 because i use overflow-y: in my CSS)

    My page looks like crap on IE6 though, so i was looking for a redirect to either warn them that the page won’t work, or just not allow access until they upgraded

    Note: complys fully with the latest standards. Either way this piece of script is definatly going to be useuful to me.

  18. Nancy says:

    I’m about to give up, no matter what I do I does not work :( this is what I’m writing:
    <!–
    var browser = navigator.appName
    var ver = navigator.appVersion
    var thestart = parseFloat(ver.indexOf(”MSIE”))+1
    var brow_ver = parseFloat(ver.substring(thestart+4,thestart+7))
    if ((browser==”Microsoft Internet Explorer”) && (brow_ver

    if I delete all the var that has something to do with the version and just leave the var browser works, but no matter how bad I would like to reject IE users I can’t so I just want to reject the IE6 and lower users… help!

  19. Ryan says:

    Hey Nancy, it’s an issue with curvy quotes (”) – change them to straight (") quotes and you should be alright. Damn WP automatically changes them.

    Optionally, simply copy and paste the code directly from the .JS file.

  20. Nancy says:

    Thank you I’ll try tomorrow, I’m too tired now, I’ll let you know

  21. Nancy says:

    It worked!!!! :) thank you very much, do you mind if I use some of your text from your oldie.html?. I love what it says and everything and I sure want people to use Firefox instead on IE, but I think my client will find it “aggressive”, but I want to use the same concept.

  22. Ryan says:

    @Nancy – glad it’s working. You are welcome to change any part of the script, text, or graphics to better suit your needs. I offer no copyright on any of it.

  23. Nancy says:

    Thank you!!!!!!!!!!

  24. marcus says:

    right on .. IE, UGH!

  25. Morten René says:

    Awesome script, when IE 6 (and older), simply fails to open strict DTD and latest WAI standard web sites. It’s good that Microsoft realised that their de facto standards does not work compared to W3C. IE7, and hopefully the upcoming IE8, works much better!

  26. Kevin says:

    Hi Ryan, thanks for this script, much appreciated.. I did notice in your comments that you were saying a better option would be to redirect to a different stylesheet instead which is what I would prefer doing. Care to enlighten us on how that would be done?

  27. Ryan says:

    Hey Kevin – check out the pingback between your comment and this one – wrote that sucker just for you ;-) (and because this blog desperately needed to be updated!).

  28. Kevin says:

    Thanks Ryan! checking it out now… :)

  29. Mikolaj says:

    Thanks a lot. IE is crap ;/

  30. D says:

    Great script, simple but perfect for I needed. I check my stats and still see that IE is most commonly used…sucks to be a developer :)

  31. kindred spirit says:

    Thank you!!! IE is the bane of my web existence. You have made my life so much easier!

  32. Asinausk says:

    Awesome! thank you so much. I no longer feel as though my face is going to implode out utter frustration. that’s a good feeling.

  33. Chris M. says:

    I found that I had to add an ‘=’ to the ‘brow_ver < 6′ to make it detect and redirect IE6.

    if ((browser==”Microsoft Internet Explorer”) && (brow_ver <= 6))

    Like that, in other words. Thanks a million for this script.

  34. Shan says:

    Thank you Ryan, super useful.

  35. Ryan says:

    Hey Shan, cheers.

    NOTE: For anyone looking for the ability to detect and redirect based on IE version, you might be interested in the latest post on this blog. It essentially does a more subtle (and easier to implement) IE detection using conditional comments.

  36. Kiten says:

    Hello, great script, thanks!

    How to insert code into WordPress blog ?

  37. Ryan says:

    @Kiten: Simply throw it in between the HEAD tags usually found in the header.php file of your theme.

  38. Kiten says:

    I mean is there any plugin for this?

  39. Ian says:

    Hi,

    Is it possible to redirect certain people to a firefox ready page? and all others to a IE ready page? Seems these two are the main browsers in the UK.

    Will this slow people down? and is it 100% relaible?

    Thanks

  40. Ryan says:

    @Kiten: None that I know of.

    @Ian: You don’t need two different pages, you can just style things differently to fix inconsistencies between the browsers. Check out a post I’ve written on how to better handle redirection using conditional comments.

  41. mileq says:

    thank you – you save my live

  42. tk says:

    Bah to ie! This was really helpful, thanks for releasing this great script!

  43. Matt says:

    Just want to let you know – love this script… Exactly what I was looking for (going to include in my new site). But I have to say, I agree with many people of having to create an alternate site for IE6, etc… So in my case I will be going that. Also, I agree with you – I HATE IE as well!

  44. haider_up32 says:

    is there any javascript which can detect ie users and warn them to download firefox

  45. amenity says:

    Ah, you’re a lifesaver. Thanks so much.

  46. Chris Lee says:

    It’s work! Thank your great script!

  47. Bazza says:

    Man, this is bloody excellent! I love you! lol

  48. Tom says:

    Thank you so much for this, I spent ages trawling around the internet for this, and this is so simple – yet so great!

    Anything to support the Bring Down IE6 campaign

    >>http://www.facebook.com/home.php#/group.php?gid=51915907298

    Thanks again

    -Tom

  49. Jim N. says:

    Hi Ryan,

    Thanks for this script. I’m having success with the redirect, but my problem is that IE6 just keeps redirecting and redirecting and the actual never loads. It’s like the script saw it was IE6, said “send it over to /ie6/” and once /ie6/ started to load, the script tried to send the user to /ie6/ again, thus never actually loading the page.

    Any idea what might be going on? Many thanks.

  50. Stone Deft says:

    YEAH this script rocks, now I can redirect to a page that will say something like: “We support the death of IE 6″. I hope IE 6 users gets the point . Thanks man you look great

  51. Rakesh says:

    Wow Ryan!! Thank you so much for the script. it works like charm. Great work! bye bye oldie!

  52. JQuest says:

    I don’t get it…the script works fin in IE simulators, but it does not distinguish between IE6 and IE7. It redirects both-regardless. I only need IE6 and below redirected, not 7 and and above. I’m not sure why it is not working…=(

  53. JQuest says:

    It’s me again…scratch that, it redirects ALL versions of IE…does not distinguish versions. Filters them all…as much as I hate Emperor Gates MicroCrap Exploder…some of the users of my site have this piece of junk as a browser, and I just want to weed out IE 6 and older-not all of them. Help please!!! I wonder if this is a Vista problem, another Microsoft Genius Product by the way…

    HELP!!

  54. Ryan says:

    @JQuest – please see the note I’ve added to the top of the post. It was a comment I left on here a while ago, but as it’s burried in the comments, I realized I need to draw more attention to it.

    Basically, check this post. It’s a more efficient way of handling things.

  55. JQuest says:

    Ryan-you are the man!!! A simple fix; why did I not think about that!!! Works like a charm now!! Thanks!!! I will follow the cause-IE is goin’ down…

  56. Sebastijan says:

    Thank you, it helps me go through css position problem in explorer browser.

  57. Thanks for your script, one of my sites http://jivova.ic.cz is now sending IE6 users to an advise page to get some newer stuff. I hated when, showing this site to people, I saw that wordpress just didn’t worked there. Maybe it’s WP issue, but I don’t seem to care.

  58. Hello Ryan,

    Just put this script on my website. Works fine. Cheers. Saved me having to redesign just for ie6 or lower. Amazingly, ie6 still has a 20% share of web users in 2010! its four years since ie7. The updates free people.

    Thanks as well for explaining how to use the script in a clear way, a rare talent for a web developer!

    Rock on.

  59. Jonesin' says:

    THANK YOU THANK YOU THANK YOU!!!!!!!! I HATE Internet Explorer too. This is what i needed.

  60. Aaron says:

    Thanks! I’ve been trying to find a script that actually worked all morning, and this is the only one I’ve found that does what I want. I set it to redirect IE6 and below to a different page on my site:

    http://one9.us/browser-redirect.html

    So far, seems to work perfectly. For anyone interested, I use http://ipinfo.info/netrenderer/index.php to check the pages.

    Thanks again!

  61. Greg says:

    Awesome, am going to use this heaps. United we stand against corporate oppression.

  62. Stefan says:

    Thank you very much, it is working. My site didn’t work at all in IE 4 5 and 6, so I needed this. From now on, i will probably use your code on most of the websites. Cheers

  63. Jonas says:

    Finally a script that works.
    I’ve tried so many and something always was going wrong.

    GOOD JOB MAN :-)

    Thanks

  64. Betty says:

    Thank you so much! The sample files seem to be gone but your instructions are thorough and I implemented my first (well, your’s really!) javascript.

  65. This requires so much work, i just use the below code in head section as far top possible.


    /* */

    works great for me

  66. Chris Wicked says:

    Just what i was looking for! Awesome! Thanks! :D

  67. Wolf says:

    Hey man. Using your script – its ace! Just one thing, I’m getting feedback from some visitors who insist they’re using IE8 (the only IE I’ll permit) yet are still getting the redirect page. I can’t get to their machines to verify their claims but I’ve had this a couple of times so I’m inclined to think they’re telling the truth. Any ideas?

  68. Ryan says:

    Hi Wolf — I recommend using this method as it allows you to target specific versions of IE a bit better.

  69. metude says:

    It looks js. html and image files are gone…

  70. omi says:

    Thank you so much. This is awesome. You really save my day.

Trackbacks/Pingbacks

Leave a Reply

Please review our Comment Policy before submitting. Submission indicates agreement and adherence to this policy.