Detect whether browser is IE 7 or lower?

Check out conditional comments.

So you can do something like:

<script type="text/javascript">
    <!--[if (!IE)|(gt IE 7)]>
      window.location = "ie.html" 
    <![endif]-->

    <!--[if lt IE 8]>
      window.location = "main.html"
    <![endif]-->
</script>

Conditional comments (as suggested by @Kon) are the way to go. Here's a working implementation:

<script type="text/javascript">
    var ie7OrLower = false;
</script>

<!--[if lte IE 7]><script type="text/javascript">
   ie7OrLower = true;
</script><![endif]-->

<script type="text/javascript">
    window.location = ie7OrLower ? "ie.html" : "main.html";
</script>

Your code is always resulting to having gone to main.html. Even when the code falls into <8, you'll fall out of the if into setting to main.

Consider refactoring by either:

  • setting a return after setting to ie.

or

var redir="main.html";
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
{ 
   var ieversion=new Number(RegExp.$1);
   if (ieversion<=8)
   {
      redir = "ie.html";
   }
}
window.location = redir;