Microsoft KB Archive/246915

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


IIS: Using <% %> Tags Instead of <SCRIPT> Tags Affects Processing Order

Article ID: 246915

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0



This article was previously published under Q246915

We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

SYMPTOMS

When using <SCRIPT RUNAT="SERVER"></SCRIPT> tags instead of or in addition to <% %> tags to denote Active Server Pages (ASP) code in a page, the processing of ASP code may appear to be out of order.

CAUSE

This behavior is by design. ASP code in <SCRIPT> tags is processed after the end of the page's HTML information, whereas ASP code in <% %> tags is processed in-line.

MORE INFORMATION

The following examples will illustrate this behavior.

  • Save the following script as Test1.asp in a folder with at least "script" access defined:

    <html>
    <body>
    <script runat="server" language="vbscript">
        Response.Write "<p>Line 1</p>"
    </script>
    <p>Line 2</p>
    </body>
    </html>
                            

    Browsing this page will output the following HTML:

    <html>
    <body>
    <p>Line 2</p>
    </body>
    </html>
    <p>Line 1</p>
                            

    The HTML output would seem at first glance to be out of order based on the location of the script inside the page.

  • Save this next script as Test2.asp in the same folder as for the previous example:

    <% @language="vbscript" %>
    <html>
    <body>
    <%
        Response.Write "<p>Line 1</p>"
    %>
    <p>Line 2</p>
    </body>
    </html>
                            

    Browsing this page will output the following HTML:

    <html>
    <body>
    <p>Line 1</p>
    <p>Line 2</p>
    </body>
    </html>
                            

    So, if you use <SCRIPT> tags for ASP, they process after the page has processed, whereas <% %> tags execute in-line. This can lead to confusion, as shown in the following scenario.

  • Save this page as Test3.asp in the same folder as earlier:

    <script runat="server" language="vbscript">
        strText = "<p>Line 1</p>"
    </script>
    <html>
    <body>
    <%
        Response.Write strText
    %>
    <p>Line 2</p>
    </body>
    </html>
                            

    Browsing this page will output the following HTML:

    <html>
    <body>
    <p>Line 2</p>
    </body>
    </html>
                            

    The text "Line 1" is not output because the variable "strText" will not be populated until after the Response.Write has already been executed.


Keywords: kbprb kbpending KB246915