Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap

Related items

un-Gratuitous Gradients

The Amazing ActiveX - Part 1

Rattling Keys and Chasing Mice With VBScript

Introduction to Visual Basic Scripting (VBScript)

Browser Redirection using VBScript

You are here: irt.org | Articles | VBScript | Browser Redirection using VBScript [ previous next ]

Published on: Saturday 12th June 1999 By: Ryan Detert


Of all the questions that I am asked and all the FAQs that I have seen, one of the most common questions asked is how to load different pages for different browsers. This question has arisen from the legendary 'Browser Wars'. This article will show you how to detect client-side browser versions and redirect the client to a web page that is best supported by their browser. And of course, all of this will be done using Microsoft's VBScript®. Please note that much of VBScript® and JavaScript have striking similarities, as you will soon see in this article, however VBScript® offers many other functionalities that JavaScript does not.

The Navigator Object

Like JavaScript, VBScript® also uses the navigator object, which contains four properties. These properties are:

Despite the navigator object offering us four properties AppName and AppVersion are the most useful, and are the ones that we will use.

The code to access these properties looks like this:


So to print the name of the browser we will use this code:

Document.Write Navigator.AppName

The Location Object

The location object, in fact, has about eight properties associated with it, however the only one that we need to concern ourselves with is the href property. We will use this property to reset the window location after we have determined the exact browser. The syntax will be:

location.href = "new URL"

Getting Squared Away

When trying to determine the client's browser, it is often easiest to first obtain the name of the browser and then search for keywords within the name. For example, we will only search for the word "Explorer" when detecting IE and "Netscape" when dealing with a Netscape browser. This will leave less of a margin for error as new versions are introduced and names change, as was the case for Netscape Navigator changing its name to Netscape Communicator.

Now, we must somehow parse the string for the crucial information that we need. To detect Netscape browsers we will look for the word "Netscape" and to detect Microsoft browsers we will look for the word "Explorer." Fortunately, VBScript® offers a built-in string parsing function similar to JavaScript's indexOf() function. It is the Instr() function and its syntax looks like this:

Instr( [start], first string, second string, [comparison datatype])

Only the First String and Second String arguments are needed but if you decide to specify a value for the Comparison Datatype, then all arguments must be specified.

Instr() Return Values

InstanceReturn Value
First String is zero length0
First String is NULLNULL
Second String is zero lengthValue of Start
Second String is NULLNULL
Second String is not found0
Second String is foundLocation of Second String within First String
Start > length of Second String0

Parsing the AppName

Now that we know about the functions and objects that we need, let's get down to business. First, let's retrieve the AppName by using:

Dim strAppName
strAppName = Navigator.AppName
For example, if you are using Microsoft Internet Explorer to view this page, the AppName for your browser is:

Now, in order to parse the AppName to detect IE we will use the Instr() function and make it not case-sensitive so that we do not have to worry about capitalization between versions. Thus we obtain:

Dim IsExplorer
IsExplorer = Instr(1, strAppName, "Explorer", vbTextCompare)

Finding the Version

This will be done in much the same way as the AppName, except I will need to introduce two more functions, the Split() and CDbl() functions.

The Split() function is used to divide a string into a specified number of substrings and then return an indexed array (starting at zero) of all the substrings. The syntax is as follows:

Split( String, Delimiter, Count, Comparison Datatype)

To better illustrate, look at the following code:

Dim MyString, MyArray
MyString = Split("VBScriptXisXfun!", "x", -1, 1)	' MyString(0) contains "VBScript".
							' MyString(1) contains "is".
							' MyString(2) contains "fun!".

Now, that we understand this function, we will separate the components of the Navigator.AppVersion by using:

Dim strVersion, dblVersion
strVersion = Split( Navigator.AppVersion, " ", -1, vbTextCompare)

We have just separated the parts of the AppVersion string into a series of substrings. However, in order to make any logical comparisons, we will need to find a way to convert the strings into numbers. CDbl() does this for us. It will take any string consisting of numbers (including decimals and commas according to what country you live) and will convert it to a double precision decimal number of datatype Double. For example:

CDbl(CDbl("4.56") + 1) = 5.56

However, CDbl("hello world") or anything like this will return zero.

The actual AppVersion should always be located at the beginning of the string. So to obtain the AppVersion we will use our previous code with the addition of the CDbl() function and get:

dblVersion = CDbl( strVersion(0) )

A Finished Example

In order to redirect a client that is using Microsoft Internet Explorer 4.00 and above we can use the following code:

Dim strAppName, IsExplorer, strAppVersion, dblAppVersion

strAppName = Navigator.AppName

IsExplorer = Instr(1, strAppName, "Explorer", vbTextCompare)

strAppVersion = Split(Navigator.AppVersion, " ", -1, vbTextCompare)

dblAppVersion = CDbl( strAppVersion(0) )

If IsExplorer AND dblAppVersion >= 4.00 Then

	Location.href = "http://www.irt.org"

End If


Although IE is the only browser that currently supports VBScript, hopefully one day Netscape browsers will also support it. VBScript has not made a huge impact yet, but with the increasing need for Visual Basic programmers, VBScript will one day become as popular as JavaScript.

Related items

un-Gratuitous Gradients

The Amazing ActiveX - Part 1

Rattling Keys and Chasing Mice With VBScript

Introduction to Visual Basic Scripting (VBScript)

©2018 Martin Webb