Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Order matters when using different languages in ASP! 3

Status
Not open for further replies.

ESquared

Programmer
Dec 23, 2003
6,129
US
I read at Does order matter when using different languages in ASP? that the answer is, indeed, yes.

Their recommendation is "don't mix <%%> and <script runat=server>. Use one or the other."

Running through their examples, here's what I extracted.

There are four types of text that you write in an ASP. Three can be executed and one cannot:

[ul][li]<script language="VBScript" runat=server>[/li]
[li]<script language="Javascript" runat=server>[/li]
[li]<% script enclosed in these tags%>[/li]
[li]<HTML>[/li][/ul]

They execute/display in this order, no matter where they are in the source:

[ol][li]The runat=server code that is NOT in the language specified at the top of the sourcesuch as <%@language="vbscript"%> no matter where it is in the source.[/li]
[li]All the HTML and <% %> enclosed script, interleaved together.[/li]
[li]Finally, the runat=server code that IS in the language specified at the top of the page no matter where it is in the source.[/li][/ol]

So the following ASP source
Code:
<%@language="vbscript"%> 
<HTML><BODY>
1 HTML<p> 
    <script language="VBscript" runat=server> 
        response.write("2 vbscript runat=server<p>") 
    </script> 
3 HTML<p>
    <script language="javascript" runat=server> 
        Response.Write("4 javascript runat=server<p>");
    </script> 
5 HTML<p> 
    <% 
        Response.write("6 inline script<p>") 
    %> 
7 HTML<p> 
    <script language="javascript" runat=server> 
        Response.Write("8 javascript runat=server<p>");
    </script> 
9 HTML<p> 
    <script language="VBscript" runat=server> 
        response.write("10 vbscript runat=server<p>") 
    </script> 
11 HTML<p></BODY></HTML>
renders as the following page:

Code:
4 javascript runat=server

8 javascript runat=server

1 HTML

3 HTML

5 HTML

6 inline script

7 HTML

9 HTML

11 HTML

2 vbscript runat=server

10 vbscript runat=server
The easiest way to remember all this is simple:

[ol][li]Don't use runat=server for the language you specify at the top of the source. In that case, just use script inside <% %> tags. Put another way, only use runat=server when you need the alternate language than that specified at the top. VBScript is the default, so you don't have to specify that.[/li]
[li]runat=server code in the alternate language will run first. Just put it at the top of the source and there will never be any confusion.[/li][/ol]

 
I want to make an additional note to this, as it could be misleading.

ASP will not execute every non-VBScript runat block in the order they are presented (assuming VBScript is specified as your language). Rather, it scans the runat blocks and builds an order of executiong for the differant languages. I assume it is building a single file of all of the blocks for that language to send to the language processor all at once, rather than every time it runs into a block.

What this means is that if you have multiple languages, ASP is going to build a list of those languages and move whichever one you have defined for your ASP script to the end of the execution list. So if you have something like:
Code:
<%@language="vbscript"%>
<HTML><BODY>
1 HTML<br/>

<script language="VBscript" runat=server>
	response.write("2 vbscript runat=server<br/>")
</script>

3 HTML<br/>

<script language="javascript" runat=server>
	Response.Write("4 javascript runat=server<br/>");
</script>

5 HTML<br/>


7 HTML<br/>

<script language="perlscript" runat="server">
	$Response->Write("12 perl runat=server<br/>");
</script>

8 HTML<br/>

<%
	Response.write("9 inline script<br/>")
%>

10 HTML<br/>

<script language="javascript" runat=server>
	Response.Write("11 javascript runat=server<br/>");
</script>

12 HTML<br/>

<script language="VBscript" runat=server>
	response.write("13 vbscript runat=server<br/>")
</script>

14 HTML<br/>

<script language="Python" runat="server">
Response.Write("15 python runat=server<br/>")
</script>

16 HTML<br/>

<script language="perlscript" runat="server">
	$Response->Write("17 perl runat=server<br/>");
</script>

</BODY></HTML>

Then ASP will build a list of:
VBScript, Javascript, PerlScript, Python, ASP Block
Then move VBScript to the end:
Javascript, PerlScript, Python, ASP Block, VBScript
Then execute all the code for each language in one go, giving you output similar to:
4 javascript runat=server
12 javascript runat=server
6 python runat=server
16 python runat=server
8 perl runat=server
18 perl runat=server
1 HTML
3 HTML
5 HTML
7 HTML
9 HTML
10 inline script
11 HTML
13 HTML
15 HTML
17 HTML
2 vbscript runat=server
14 vbscript runat=server

The idea behind this process is that your probably only defining functions in your blocks for the other languages to do things that are not available in your primary language. Those blocks need to be pre-processed in order to register the functions in them before the core ASP code is executed that is going to be referencing them.
I'm not really sure why the runat block in the same language is run last, unless ASP can only handle having a single ISAPI call setup for a language, causing the system to have to finish executing everything in that language before doing the evaluation and execution of another section in that same language.

ESquared: Thanks for the informative post. I have to admit I don't play with runat blocks often and had never noticed this in the past.

 
You're welcome, Tarwn. And thanks for your own additions. I hadn't thought about other scripting languages, but the behavior is what I would have assumed from my experiment (although I hadn't proved that).

I'm just getting into professional ASP development for the first time on a real project (I'd only dabbled before) and I like to share what I learn, especially when it seems important, or took a lot of web searching, or was really new to me and surprising or nonobvious even though I have a pretty long vbscript & vb programming background.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top