Page

2.3.0- The Order of Execution

Created by Brendan Doss.
Last Updated by Jim Minatel.  

<< 2.2.4- Client-Side Scripts Chapter2 2.4.0- What does ASP do for us? >>

The Order of Execution

Now we've spent time understanding server-side and client-side scripts, and the role of the script hosts and script engines, we can write a series of examples which will allow us to explore the mysterious 'order of execution' a little further.

Try It Out – Testing the Order of Execution

For the first of these examples, we'll create a script that writes a series of ten lines of text to the browser. However, some of these lines will be written using pure HTML; some will be written using ASP (as indicated by the <%%> tags); and some will be written using server-side VBScript (as indicated by the <SCRIPT  LANGUAGE=VBSCRIPT  RUNAT=SERVER> tag).

 

1.    Open your HTML editor, create a new file, and type the following code:

<HTML>

<HEAD>

<TITLE>Testing the Order of Execution</TITLE>

</HEAD>

 

<BODY BGCOLOR=WHITE>

Line 1: First HTML line<BR>

<% Response.Write "Line 2: First ASP line<BR>" %>

Line 3: Second HTML line<BR>

<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>

  Response.Write "Line 4: First server-side VBScript line<BR>"

</SCRIPT>

Line 5: Third HTML line<BR>

<% Response.Write "Line 6: Second ASP line<BR>"%>

Line 7: Fourth HTML line<BR>

<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>

  Response.Write "Line 8: Second server-side VBScript line<BR>"

</SCRIPT>

Line 9: Fifth HTML line<BR>

<% Response.Write "Line 10: Third ASP line<BR>"%>

</BODY>

</HTML>

 

As you can see, some of these lines will be interpreted by the ASP script host, and some directly by the server-side VBScript script engine. We've numbered these ten lines of text, and each one indicates the method that we've used to write the line.

 

2.    Save the file as ExecOrder1.asp in your \Inetpub\wwwroot\BegASPFiles directory. Since this page contains ASP code, we must save the file using an .asp extension.

3.    Go back to your browser and type the address http://my_server_name/
BegASP/ExecOrder1.asp
into the address line. Don't forget to specify the name of your web server. If you follow these instructions you should get something that looks like this:

 

Chapter2_image013

 

This is perhaps not exactly what you expected – after all, we did write lines 1 to 10 in the right order in the code! But we can use this result to determine which parts of the code are being interpreted in which order.

How It Works

As we noted at the start of the example, we've used three different methods to write different bits of the page's content:

 

  • Pure HTML – for example, Line 1: First HTML line<BR>
  • ASP code – for example, <% Response.Write "Line 2: First ASP line<BR>"%>
  • Server-side script in a <SCRIPT> tag – for example,
  • <SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>
    Response.Write "Line 4: First server-side VBScript line<BR>"
    </SCRIPT>

 

The unexpected order of the lines as they are displayed on the browser is due to the order in which the server processes the different elements of the code. The first of these methods doesn't require any processing on the part of the web server – it's pure HTML. The second and third parts both use the ASP command Response.Write, which allows to us to write things into the HTML stream that will be sent back to the browser. That's not the important issue in this example – in fact, we'll discuss Response.Write in depth in chapter 3 . What's more important here are the differences between these two methods, and how they relate to the web server's order of execution.

 

To appreciate what the web server has done, let's take a look at the HTML source for the page. To do this in an IE browser, select View | Source from the browser's menu. In Netscape browsers, select View | Page Source. Here's what I get:

 

<HTML>

<HEAD>

<TITLE>Testing the Order of Execution</TITLE>

</HEAD>

 

<BODY BGCOLOR=WHITE>

Line 1: First HTML line<BR>

Line 2: First ASP line<BR>

Line 3: Second HTML line<BR>

 

Line 5: Third HTML line<BR>

Line 6: Second ASP line<BR>

Line 7: Fourth HTML line<BR>

 

Line 9: Fifth HTML line<BR>

Line 10: Third ASP line<BR>

</BODY>

 

</HTML>

Line 4: First server-side VBScript line<BR>Line 8: Second server-side VBScript line<BR>

 

There are two important lessons we can get from this. First, note that lines 1, 2, 3, 5, 6, 7, 9, 10 appear in the HTML stream in the same order that we coded them originally. These are the lines that we wrote in pure HTML, and in <%%>-delimited ASP. This tells us that we can mix our pure HTML and our <%%>-delimited ASP in the original code as much as we like – and the web server will ensure that the order in which we code these things will be preserved.

In other words, we can write a mixture of bits of pure HTML and bits of 'in-line' ASP code, and the web server won't mess with that order. That's an important point – and one that we'll use to our advantage many times during this book.

 

There's a second lesson to learn here. It's pretty clear that, in the HTML source, lines 4 and 8 are missing from their original order – instead, they are appended at the end of the HTML source. Essentially, this is because the web server runs through the original code twice when it's generating the HTML to be sent to the browser. The first time, it ignores any script of the form <SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>, but handles other things (like pure HTML and ASP code); the second time it revisits those scripts, interprets them in order, and appends the resulting HTML onto the end of the existing HTML.

 

So, how is the order of execution affected if we try adding some JScript scripts? Let's try it out by making a small change to the example above.

Try It Out – Test the Order of Execution Using JScript

1.    Copy the file ExecOrder1.asp to a new file; call that file ExecOrder2.asp and make the following changes to it:

 

<HTML>

<HEAD>

<TITLE>Testing the Order of Execution</TITLE>

</HEAD>

 

<BODY BGCOLOR=WHITE>

Line 1: First HTML line<BR>

<% Response.Write "Line 2: First ASP line<BR>" %>

Line 3: Second HTML line<BR>

<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>

  Response.Write "Line 4: First server-side VBScript line<BR>"

</SCRIPT>

Line 5: Third HTML line<BR>

<% Response.Write "Line 6: Second ASP line<BR>"%>

Line 7: Fourth HTML line<BR>

<SCRIPT LANGUAGE=JSCRIPT RUNAT=SERVER>

  Response.Write ("Line 8: First server-side JScript line<BR>");

</SCRIPT>

Line 9: Fifth HTML line<BR>

<% Response.Write "Line 10: Third ASP line<BR>"%>

</BODY>

</HTML>

 

As you can see, it's quite similar to the previous example. All we've done is replace one of the server-side VBScript scripts with a server-side JScript script.

 

2.    Save the ExecOrder2.asp file, and view it on your browser. The result, even more surprisingly, is this:

 

Chapter2_image014

 

As you can see, the web server renders the JScript script first. Then it renders the HTML code and any code contained within <% ... %> delimiters, again ensuring that the code is processed 'in-line'; and the script contained within the <SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER> tags is rendered last.

 

3.    Now make the following changes to ExecOrder2.asp, and save the these changes as ExecOrder3.asp:

<%@ Language = JScript%>

<HTML>

<HEAD>

<TITLE>Testing the Order of Execution</TITLE>

</HEAD>

 

<BODY BGCOLOR=WHITE>

Line 1: First HTML line<BR>

<% Response.Write ("Line 2: First ASP line<BR>") %>

Line 3: Second HTML line<BR>

<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>

  Response.Write "Line 4: First server-side VBScript line<BR>"

</SCRIPT>

Line 5: Third HTML line<BR>

<% Response.Write ("Line 6: Second ASP line<BR>") %>

Line 7: Fourth HTML line<BR>

<SCRIPT LANGUAGE=JSCRIPT RUNAT=SERVER>

  Response.Write ("Line 8: First server-side JScript line<BR>");

</SCRIPT>

Line 9: Fifth HTML line<BR>

<% Response.Write ("Line 10: Third ASP line<BR>") %>

</BODY>

</HTML>

In this version, we've specified the ASP scripting language to be JScript (rather than the default, VBScript), and amended the syntax of the <%%>-delimited lines accordingly.

 

We won't be choosing JScript for our ASP scripts again in this book – but it's worthy of a mention. For the remainder of the book (after this example) we'll stick to using VBScript, the default scripting language for ASP.

4.    Now view ExecOrder3.asp in your browser:

 

Chapter2_image015

 

As a result of specifying JScript as the scripting language for ASP, lines 4 and 8 have been processed in a different order.

How It Works

ASP does not guarantee the order of execution of script written in different script blocks <SCRIPT RUNAT=SERVER>. At present it seems that the web server first processes scripts written in the language other than your ASP default language; and processes scripts written in your ASP default language at the end. In between, script written within <%%> delimiters are always processed in-line – relative to their position in the surrounding HTML.

 

If you write a script within a <SCRIPT RUNAT=SERVER> script block, it will be processed either at the beginning or at the end – it won't get processed in-line. Therefore, such script blocks are generally reserved for writing structures called procedures and functions – pieces of code that are separate from the main code for the page, and which are "called" into the page. We'll meet these later in the book.

 

In general, we'll be specifying server-side script using the <%%> delimiters – so these scripts are processed through the ASP script host, and are processed in-line.

<< 2.2.4- Client-Side Scripts Chapter2 2.4.0- What does ASP do for us? >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |