Published on: Saturday 12th June 1999 By: Ryan Detert
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:
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"
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.
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.
|First String is zero length||0|
|First String is NULL||NULL|
|Second String is zero length||Value of Start|
|Second String is NULL||NULL|
|Second String is not found||0|
|Second String is found||Location of Second String within First String|
|Start > length of Second String||0|
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
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)
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) )
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