Page redirect with JavaScript

Sometimes, you may run into a situation where some part or all of your web page is not properly viewable in a particular browser. If such situation arises, the script below shows how to redirect the browser based on its version number to a particular web page. The script assumes that you have two versions of the same page. Ideally, one page would be designed for older browsers and the other page is for new browsers.

Scripting code

Step 1: place the following code in the head section (between <head> and </head>) of your web document:

<script language="javascript">
function BrowserVer4BasedRedirect() {
var URL;
// check browser version
if(navigator.appVersion.substring(0,1) < 4) {
// URL set to a page that is compatible with user's browser version of 4 or less
// make sure your destination page exists!!!
URL = "pageForBrowserVer4.html";
}
else {
// URL set to a page that is compatible with user's browser
// version of higher than 4
URL = "pageForBrowserVer4OrHigher.html";
}
window.location = URL;// redirect
}
</script>

The BrowserVer4BasedRedirect () function simply uses a conditional if statement to check if the browser version is 4 or greater. If the browser is greater than 4, the URL variable is set to “pageForBrowserVer4.html.” Otherwise, the URL variable is set to “pageForBrowserVer4OrHigher”. Finally, the window.location is set to the value of the variable URL. By doing that, the browser’s address bar will change to the web page that the variable URL holds and the browser will attempt to request that page.

Step 2: Call the above function (BrowserVer4BasedRedirect()) from your web page:

<script language="javascript">
BrowserVer4BasedRedirect(); // calling the function
</script>

To be able to use any function, we must execute it. This is what we do in step 2: we instruct the browser to execute BrowserVer4BasedRedirect () function.