Using VBScript with JavaScript
Get Started:
You can get JavaScript and VBScript to sing from the same sheet of music. Consider the script below.
<%@LANGUAGE="JavaScript"%> <SCRIPT LANGUAGE="JavaScript" RUNAT="Server"> function JSGreeting() { return "Greetings from a JavaScript Function"; } </SCRIPT> <SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Function VBGreeting() VBGreeting="Greetings from a VBScript Function" End Function Function toDollars(x) toDollars=FormatCurrency(x) End Function </SCRIPT> <% var a = 2; var b = 2; var c = add(a,b) c += " (Two numbers are added by JavaScript, " c += "and then formatted into currency by VBScript.)" function add(x,y) { result = x + y; result = toDollars(result); return result; } Response.Write("<HTML>/r") Response.Write(JSGreeting() + "<BR>/r") Response.Write(VBGreeting() + "<BR>/r") Response.Write(c + " <BR>/r") Response.Write("</HTML>/r") %>
Click Here to run the script in a new window.
RUNAT:
Let me pick this apart from top to bottom. Directly below is a simple stand-alone JavaScript.
<SCRIPT LANGUAGE="JavaScript" RUNAT="Server"> function JSGreeting() { return "Greetings from a JavaScript Function"; } </SCRIPT>
This looks like any simple JavaScript you might find on the client side except for this little number: RUNAT="Server".
The RUNAT attribute tells the server to execute on the server, so the client never sees it. You can also set RUNAT="Client" in which case the Script would be passed along as-is to the web browser.
By the way, <SCRIPT LANGUAGE="JavaScript" RUNAT="Server"> should NOT be put inside ASP flags.
Take a look at the VBScript directly below.
<SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Function VBGreeting() VBGreeting="Greetings from a VBScript Function" End Function
This VBScript does exactly the same thing as its JavaScript counterpart. There's nothing terribly special about it.
The Invocation:
There is, however, something special about the following VBScript.
Function toDollars(x) toDollars=FormatCurrency(x) End Function
The above function will take an argument from a JavaScript Function. Then it will return the appropriate value to the JavaScript function. Let me show how Function toDollars(x) is invoked.
function add(x,y) { result = x + y; result = toDollars(result); return result; }
function add(x,y) is a JavaScript function. Halfway into the function, there is the following line:
result = toDollars(result);
The above line is in fact a call from a JavaScript function to invoke a VBScript function. How about that!
Warning:
Just one caveat. When you create scripts using the RUNAT attribute there is in fact an order of execution. I won't go into it now, because the order of execution is situational.
Here's what I will get into: Use scripts with the RUNAT attribute only for functions (and those global variables called inside functions). DON'T put top level code into it. You have no guarantee that top level code will execute in order using this technique.