Detect & Redirect Script For Internet Explorer (IE)

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.

Filed under: CSS/HTML, Code & Scripts | Tags: , , , | Written on June 30, 2007

36 Comments »

Comment by
Levi

awesome, thanks!

August 11, 2007 @ 1:53 am

Comment by
Jerry Sparrow

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

August 13, 2007 @ 11:26 pm

Comment by
Ryan

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.

August 13, 2007 @ 11:36 pm

Comment by
Jerry Sparrow

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

August 15, 2007 @ 11:54 pm

Comment by
Ryan

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.

August 16, 2007 @ 7:42 am

Comment by
erik

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.

September 4, 2007 @ 2:33 pm

Comment by
Ryan

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. :-)

September 4, 2007 @ 4:16 pm

Comment by
Snadowitz

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

September 24, 2007 @ 5:21 pm

[...] was just reading Ryan’s post over at DaobyDesign about how he guides his readers to upgrade to at least internet explorer 6: My parameters were simple. If the person visiting the site had any browser other than Internet [...]

October 8, 2007 @ 10:35 pm

Comment by
Valehru

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.

October 11, 2007 @ 11:57 pm

Comment by
Ryan

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.

October 12, 2007 @ 8:07 am

Comment by
Valehru

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.

October 12, 2007 @ 6:19 pm

Comment by
Ryan

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.

October 12, 2007 @ 7:46 pm

Comment by
Tom

DONT WORK!!! :(

November 19, 2007 @ 10:33 pm

Comment by
Ryan

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

November 20, 2007 @ 12:07 am

Comment by
Darren

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.

December 21, 2007 @ 12:05 pm

Comment by
Ryan

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.

December 21, 2007 @ 9:48 pm

Comment by
Darren

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.

December 21, 2007 @ 11:49 pm

Comment by
wowfood

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.

April 7, 2008 @ 7:37 pm

Comment by
Nancy

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!

April 10, 2008 @ 9:52 am

Comment by
Ryan

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.

April 10, 2008 @ 10:52 am

Comment by
Nancy

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

April 10, 2008 @ 11:29 am

Comment by
Nancy

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.

April 11, 2008 @ 8:59 am

Comment by
Ryan

@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.

April 11, 2008 @ 9:12 am

Comment by
Nancy

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

April 11, 2008 @ 10:38 am

Comment by
marcus

right on .. IE, UGH!

April 11, 2008 @ 5:05 pm

Comment by
Morten René

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!

April 21, 2008 @ 6:08 am

Comment by
Kevin

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?

May 23, 2008 @ 3:03 am

[...] while back I whipped up a little script to detect and redirect visitors if they were using an old version of IE. In the comments I went on to say that a more effective/professional way of handling IE users is [...]

May 23, 2008 @ 11:46 am

Comment by
Ryan

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!).

May 23, 2008 @ 11:52 am

Comment by
Kevin

Thanks Ryan! checking it out now… :)

May 23, 2008 @ 11:58 am

Comment by
Mikolaj

Thanks a lot. IE is crap ;/

June 6, 2008 @ 1:32 am

Comment by
D

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 :)

June 6, 2008 @ 12:28 pm

Comment by
kindred spirit

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

June 13, 2008 @ 2:44 am

Comment by
Asinausk

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.

June 21, 2008 @ 4:22 am

Comment by
Chris M.

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.

July 24, 2008 @ 12:23 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

PIGEONholed

RECENTLYspoken

  • Chris M. Said: I found that I had to add an ‘=’ to the ‘brow_ver < 6′ to make it detect...
  • erik Said: Hello, I have installed the plugin and enabled it and I am able to configure the tool. When is it writing...
  • Anton Said: To Elim: the only way to get rid of the patch is overwrite your whole joomla installation with default...
  • dış cephe Said: thanks andrew..
  • Ryan Said: @randominquiror: Sorry RI, should be a standard Article/Article Layout menu item.

BLOGroll

PASTposts

BADGEpatch

Blog Directory - Blogged Internet Blogs - BlogCatalog Blog Directory
Copyright © 2008 Dao By Design, All Rights Reserved.