Use JavaScript to Forward to Specific Pages Based Upon Browser Version

A slightly more technical article for today.

Today, I was asked by a colleague to come up with a snippet of code that would detect the version of Internet Explorer in use on a computer and then forward the browser to different pages based upon the result. With a bit of help from Google, I cobbled together the following:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en” xml:lang=”en”>
<head>
<title></title>
<script language=”JavaScript” type=”text/javascript”>
function msieversion()
{
var ua = window.navigator.userAgent
var msie = ua.indexOf ( “MSIE ” )

if ( msie > 0 )      // If Internet Explorer, return version number
return parseInt (ua.substring (msie+5, ua.indexOf (“.”, msie )))
else                 // If another browser, return 0
return 0
}
</script>
<script language=”JavaScript” type=”text/javascript”>

if (msieversion() <= 7)
{
window.location=”http://google.com.au”;
}
else
{
window.location=”http://bing.com”;
}
</script>
</head>
<body> </body>
</html>

You can change or extend it to suit your needs or if you just need inspiration to do something else that requires a similar sort of interaction between scripts. You could change it to detect other browsers and their corresponding versions or replace the page redirection script with something else entirely that relies upon the information gathered in the first script.

Sharing is caring!

Leave a Reply

Your email address will not be published.