<%@LANGUAGE=VBScript%> Mixing server-side JavaScript and VBScript on IIS

Mixing server-side JavaScript and VBScript on IIS

Background

When writing ASP code to be served from IIS, you have two choices for the language to use: JavaScript (aka JScript) and VBScript. Most people put a single call like:
<%@LANGUAGE=JAVASCRIPT%>
at the top of the page, use the short ASP tags of <% %> to encapsulate their code, and are done with it.

However, ASP pages can actually use a combination of both languages if you so desire (or perhaps I should say, "If you need to") by using server-side script blocks such as:

<script runat=server language=vbscript>
...
</script>

In an attempt to discover how and when you may variables and functions from one block of code in another, this test page was created and the following conclusions drawn. [See the test code and its output below the conclusions.]

Note that after writing this page, I was pointed to a page on MSDN which covers much of this information.

Conclusions

  1. Code in <script runat=server> blocks is run at a different time from the code in <% %>.
    Specifically, the order of execution is as follows:
    1. <script runat=server> code from non-default language.
    2. <% %> code.
    3. <script runat=server> code from default language.

    As a slight exception to the above, functions written in a <script runat=server> block in the non-default language may access variables defined in <% %>, provided the function is called from within <% %>.

  2. As a consequence of the above variables declared in default-language <script runat=server> block are not accessible outside that block.
    This means that other VBScript or JScript <script runat=server> blocks cannot see these variables, nor can <% %> code blocks.
  3. JavaScript functions and variables defined in <script runat=server> blocks may be called or referenced from inside vbscript <% %> tags.
    The reverse is not true. (Code from <% %> may not be used in <script runat=server> because of #1 above.)
  4. JavaScript <script runat=server> functions and variables accessed from within VBScript <script runat=server> or <% %> code blocks are case-insensitive.
    With something like
    function DoTheRightThing(){...}
    declared in JavaScript, you can call the function in VBScript with any of
    DOTHERIGHTTHING()
    dotherightthing()
    dOtHerIGHTthInG()
    1. Variables defined in one JavaScript <script runat=server> and referred to in another JavaScript <script runat=server> block are case sensitive.
      If it was defined as var foo then you may not refer to it as Foo or FOO in another JavaScript block.
    How IIS handles namespace conflicts between two JS variables with the same name, but different capitalization, is not known.

Test Code

<%@LANGUAGE=VBScript%>

<%
	Dim fooVB
	fooVB = "fooVB"
%>


<SCRIPT LANGUAGE=VBScript RUNAT=SERVER>
	Dim fooVB1
	fooVB1 = "fooVB1"
</SCRIPT>


<SCRIPT LANGUAGE=JavaScript RUNAT=SERVER>
	var fooJS1 = "fooJS1";
	//var fooFromVB = fooVB1+" was readable in JS1"
	//The previous line doesn't work--JS cannot get at the fooVB1 variable.
	//See #1 above
</SCRIPT>


<SCRIPT LANGUAGE=VBScript RUNAT=SERVER>
	Dim fooFromJS
	fooFromJS = fooJS1+" was readable in VB2"
	'The previous line does work, but as per #2 above,
	'this variable can never be used by the <% %> code below.
	fooFromVB = fooVB1+" was readable in VB2"
	Response.Write(fooFromJS)
	'Note that the previous line takes effect at the VERY end of the page, after </html>
</SCRIPT>

<SCRIPT LANGUAGE=JavaScript RUNAT=SERVER>
	var fooFromJS2 = fooJS1+" was readable in JS2"
</SCRIPT>

<SCRIPT LANGUAGE=JavaScript RUNAT=SERVER>
	function OneThroughTen(){
		for (var i=0;i<10;i++) Response.Write(i+"<br>");
	}
	function WriteFoo(){
		Response.Write(FOOVB+"<br>\n");
		if (Response.Buffer) Response.Flush();
	}
	function WriteFooVB1(){
		Response.Write(FOOVB1+"<br>\n");
		if (Response.Buffer) Response.Flush();
	}
	function WriteFooJS1(){
		//Note that case matters for the following js variable
		//"FOOJS1" does not work here
		Response.Write(fooJS1+"<br>\n");
		if (Response.Buffer) Response.Flush();
	}
</SCRIPT>


<pre>
	<%=fooVB%> aka <%=FOOVB%><br>
	<%=fooVB1%> aka <%=FOOVB1%><br>
	<%=fooJS1%> aka <%=FOOJS1%><br>
	<br>
	fooFromVB : <%=fooFromVB%><br>
	fooFromJS : <%=fooFromJS%><br>
	fooFromJS2 : <%=fooFromJS2%><br>
</pre>

<%
	ONETHROUGHTEN()
	WRITEFOO()
	WRITEFOOVB1()
	WRITEFOOJS1()
%>

Test Output

No longer available as this server is no longer on IIS