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 full code to make this happen, here you go:

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.

121 thoughts on Detect & Redirect Script For Internet Explorer (IE)

  1. Pingback: China Blogging - Living in China, studying Chinese, web design and development. » Google Referrals: Detect readers using IE, guide them to install Firefox
  2. Pingback: Using Conditional Comments to Catch IE | Dao By Design Blog
  3. Pingback: Hate Internet Explorer? Take a stand against IE6 browser | Dao By Design Blog
  4. Pingback: Internet Explorer: Matando diseños CSS | Asociación Libre de Ideas
  5. Pingback: yedmart.com/blog » Blog Archive » Internet Explorer redirect script
  6. Pingback: GirlyBlogger » Blog Archive » Internet Explorer: Matando diseños CSS
  7. Pingback: Malcom McCutcheon » Blog Archive » Just Say No to Microsoft Internet Explorer
  8. Pingback: Détecter et rediriger les versions d'Internet Explorer en utilisant JavaScript | IL Design - Conseils et Tutoriels

Say something...

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>