VBScript, a.k.a. Visual Basic Scripting Edition, remains mostly unused by Web authors because of its major weaknesses:
- It is supported only by Internet Explorer.
- It is supported only on the Windows OS.
- It appeals only to seasoned Visual Basic programmers, and not the neophyte HTML-oriented scripter.
Then, why bother with it?
The different Document Object Models of the browsers make the core language similarities almost irrelevant, since we must branch to different statements to perform the same tasks for Navigator and Explorer.
Therefore, the above question can be reformulated as: Why not bother with it?
We execute conditional statements anyway, so why not add additional fuctionality to our scripts, or our Web pages, if we can? Provided, of course, that we do it simply, and cleanly, without harming the performance of non-conforming browsers?
Also, most intranets are browser-specific, so VBScript may be a way to enhance performance in Explorer-only environments.
Is It Really That Good?
String FunctionsIn the following example, the INPUT box contains a string with trailing spaces. How do we get the true length of the string? We very often need to do this when processing user form input: Click the first button. The alert box gives us the incorrect length.
The third button (IE-only) calls built-in VBScript functions resulting in the correct answer with much less code. The script/HTML for the above looks like this:
The RTrim() function takes a string argument and returns the string with the trailing spaces removed. The Len() function is, of course, similar to the JS length() method. Other useful VBScript string functions include:
- Ltrim(str) - returns string without leading spaces
Right(str,num_chars) - returns num_chars characters from the string, starting from right side (end of string);
Trim(str) - returns string without leading and trailing spaces
StrReverse(str) - returns the string with the characters reversed
Math FunctionsThe following text box contains a decimal integer to be converted to a hexadecimal value: Both buttons give us the correct value. Compare the script that's necessary, however:
There are many other useful features in VBScript, including its event handling standard and unique Date constants.
So, first let's look at how communication is established.
Produced by Peter Belesis andAll Rights Reserved. Legal Notices.
Created: Nov. 18, 1998
Revised: Nov. 18, 1998