Using Server-Side VBScript with Client-Side JavaScript | 2
[previous] |
Using Server-Side VBScript with Client-Side JavaScript
Passing a Variable
This can be a bit tricky. One of the reason I like to use JavaScript and VBScript together is that since I work in an Active Server Page environment there are things that are better done on the server and VBScript handles these chores very well. But sometimes there are things I want done that are better handled on the client and JavaScript does most of these things very well. Obviously, if we can pass information back and forth the two languages become very powerful indeed. The trick is in the timing. Server-side VBScript is resolved before the page is presented to the browser; client-side JavaScript cannot be run until after a page is presented.
You can write JavaScript to use VBScript objects, for instance, populating an array:
ArryTest[1] = <%=objSomevbobject%>;
But the value of objSomevbobject is determined by the server before the page is presented. If the value of objSomevbobject is 5, then the client sees:
ArryTest[1] = 5;
Do not forget to add error checking to ensure objSomevbObject actually has a value. In this case if objSomevbObject is null the result would be:
ArryTest[1] = ;
Not very pretty and the results can be unpredictable. It gets worse if you are calling JavaScript functions with objects that resolve to null, or if for instance, a data transfer interrupted the creation of an object.
Script error has occurred.
Object not found.
Look familiar? If you have already attempted to use these scripting languages together you have probably seen some of these errors. If you haven't, all it takes is a quick trip through the wonderful Web to see sites that forgot to check if their objects exist before using them. The following is an example of how NOT to mix the scripting languages.
First we declare a normal array and give the first element a value. Looks innocent enough.
<script langauge=JavaScript>
var arryError = new Array;
arryError[1] = "This is an error";
</script>
Now switching to VBScript we want to set the value of sMessage equal to the contents of the first element of the array 'arry' we just created.
<%
Dim sMessage
sMessage = %> arryError[1]
<%
response.write("sMessage = " & sMessage & "<BR>")
%>
Now lets see if this will work. It should right? We have a valid JavaScript array. Our VBScript tags are properly denoting which is VBScript. But if you run this you will get the following:
Microsoft VBScript compilation error '800a03ea'
Syntax error
test.asp, line 10
sMessage =
Why? Because VBScript is evaluated first so the code does not "see" the JavaScript value. Timing is everything.
Now lets show you an example that works.
Lets set up a new function with some VBScript inside.
<script language=Javascript>
function fnWorks(){
<%
dim sMessage
sMessage = "This is a test"
%>
alert('<%=sMessage%>');
}
</script>
We can call this JavaScript function in our code like this:
<a href="javascript:fnWorks();">This will work</a>
This one works. Why? The JavaScript that is run has been presented to the client. It is complete. The VBScript which was evaluated first properly substitutes "This is a test" in the function where we used the VBScript variable sMessage. If you do a view source of the resulting page you will see the following:
function fnWorks(){
alert('This is a test');
}
Again  timing is everything.
Now that we have seen some of the pitfalls let's examine how we can pass information between the two scripting languages.
You can use cookies as we saw above, or another way is to use name-value pairs on the url. Here is an example where will pass a name on the url:
<script language='Javascript'>
function fnNameNLoad(){
// I declare your name to be webghod
parent.frames[1].location = 'somenewpage.asp?newname=' + Âwebghod'
}
</script>
<BODY onload="fnNameNLoad()">
When the new page is loaded we can pull the value of newname off the url using the querystring object.
<%
Âretrieve the variable from the url
myName = request.querystring("newname")
Âwrite a special message to our guest
Response.Write("Greetings " & newname & " thou art powerful indeed.")
%>
As you can see, once we have passed the variable to the new page we can then use it like any other variable.
Summary
As we have seen VBScript and JavaScript are both very powerful. But each of these scripting languages has limitations. Trying to make the best use of each language's strengths is why I first explored getting them to work together. Sharing information between scripting languages via variables and cookies can make for very powerful solutions once you understand the timing and scope of how these constructs are used.
Keep the questions and comments coming. If I can pull myself out of Asherons Call long enough I might even take some time to answer. ;-)
Happy scripting!
About the author: Jerry Gray is a Web Application Developer with Consumers Energy and has a MS in MIS from the University of Montana. He can also be found on the FrostFell server in Asherons Call  look for Angmar Witch-King. Jerry Gray can be contacted at grgray@cmsenergy.com
[previous] |
Created: February 12, 2001
Revised: February 21, 2001

Find a programming school near you