A reader wrote me recently to describe a problem that I used to hear fairly often:
I write ASP code using VBScript and a coworker of mine uses JScript. We were wondering if we could combine our code in ASP. However, it does not seem to be working right. Here is an example of where we are having trouble: <SCRIPT LANGUAGE="JScript" RUNAT="Server">Response.Write("<H1>header</H1>");</SCRIPT><SCRIPT LANGUAGE ="VBScript" RUNAT="Server">Response.Write "<H1>body</H1>"</SCRIPT><SCRIPT LANGUAGE ="JScript" RUNAT="Server">Response.Write("<H1>footer</H1>");</SCRIPT> As you can see this code should write: headerbodyfooter but the ASP engine does not render it like that.
I write ASP code using VBScript and a coworker of mine uses JScript. We were wondering if we could combine our code in ASP. However, it does not seem to be working right. Here is an example of where we are having trouble:
<SCRIPT LANGUAGE="JScript" RUNAT="Server">Response.Write("<H1>header</H1>");</SCRIPT><SCRIPT LANGUAGE ="VBScript" RUNAT="Server">Response.Write "<H1>body</H1>"</SCRIPT><SCRIPT LANGUAGE ="JScript" RUNAT="Server">Response.Write("<H1>footer</H1>");</SCRIPT>
As you can see this code should write:
headerbodyfooter
but the ASP engine does not render it like that.
My advice: don't mix languages on one ASP page because, as my correspondent discovered, it doesn't work the way you think it should. The rendering comes all out of order. Why?
Before you read on, if you don't understand the ASP compilation model you should read my earlier post on the subject. To briefly recap, ASP translates the page into a script, sticks the script into an engine, and then clones the engine a bunch of times if it needs to. Every time the page is served, the engine is pulled out of the engine pool, the script is run, and everything just works.
So what happens when you have two languages on the page? You need two engines, but the mechanism is the same. The page is compiled up into two scripts, and ASP runs one engine and then the other.
Now it should become clear why this appears to run out of order. From ASP's perspective, the page above turns into two scripts:
// JscriptResponse.Write("<H1>header</H1>");Response.Write("<H1>footer</H1>");
' VBScriptResponse.Write "<H1>body</H1>"
The ASP engine does not maintain any information about which chunk should execute before another. It gathers up all the script chunks for each language, compiles them, and then runs each engine in turn. Not only does it lead to seemingly out-of-order execution as shown here, but it also restricts the language blocks from calling each other's methods (because the methods in the not-yet-executed block may not have been initialized yet.) A detailed discussion of how IE solves this problem deserves another blog entry on its own.
Mixing languages can also lead to performance problems, because it means that each page is now consuming TWO engines from the engine cache, which means more cache misses, larger working set, etc.
While I'm on the subject, note that it is in general a bad idea to put a whole lot of "inline" code into <SCRIPT> blocks like that. Ideally you want the server side <SCRIPT> blocks to contain only global function definitions, and the <% %> blocks to contain only "inline" code. ASP does not enforce that constraint, but ASP.NET does. (The reasoning behind that is long enough to require another post.)
If you really want to have multiple languages on one ASP page, here is a way you can make this work though if you really need to. You could write a bunch of Windows Script Component objects in different languages, which expose methods. You could then write an ASP page in one language that calls those methods in the right order. WSCs are good in ASP because (a) they efficiently cache engine state, just like ASP, and (b) they can use the ASP object model directly if you write them correctly.
I just wanted to thank Ricardo Araujo for his solution on expiration date with JScript, before knowing it i've been writing manually the correct date format... horrible!
How does (w/c)script implement execution of WSFs then?
For instance, the following code is legitimate, and executes in order of writing:
<job>
<script language="VBScript">
MsgBox "Hello, VBScript World!"
</script>
<script language="JScript">
WScript.Echo('Hello, JScript World!');
MsgBox "Hello, VBScript World, Again!"
</job>
The same is true for Function calls too:
Sub Func1(i)
If i = 1 Then
Func2()
Else
MsgBox("At Last!")
End If
End Sub
function JFunc(k) {
Func1(k)
}
Sub Func2()
JFunc(2)
JFunc(1)
Why couldn't ASP act the same? Why can't it do it now?
> Why couldn't ASP act the same? Why can't it do it now?
Because ASP and WSH (and Internet Explorer) have completely different compilation models. When the first page request comes in for a page, ASP:
* transforms the page into a series of script blocks
* compiles each block into the appropriate engine
* caches the compiled engine state
* runs each engine, one after the other.
When the second request comes in, ASP skips the first three steps and just does the last.
ASP could do it the WSH/IE way, but it would have to recompile every page from scratch every time. That consumes time and memory, and ASP is designed for speed.
With WSH/IE, there is no "second time". Every time a script runs is the first time, so there is no reason whatsoever to try to eke out more performance by caching compiled state and deferring execution until all the state is known. WSH/IE simply compiles and runs each block as it sees it, rather than doing all the compilation first, then all the running.
I don't using more script blocks prevents you from caching their compiled states. Let's say we have:
<script language"VBScript" runat="server">
WriteOut(sHeader)
<script language"JScript" runat="server">
WriteOut(sContent)
WriteOut(sFooter)
Why can't we create THREE engines, one per script block, CACHE their compiled states, and run them SEQUENTIALLY? Of course, in this case, 20 script blocks would turn to 20 engines, but that's the ASP programmer's problem. Why shouldn't the infrastructure ALLOW him combining different languages?
It's always struck me as a little bit odd that Active Server Pages, a web server, encourages developers to use VBScript and JScript to write server-side scripts. I mean, the whole point of a web server is that it produces complex strings as blindingly