Friday, March 21, 2014

VBScript Conditional Includes

VBScript Conditional Includes

This is a nasty one to track down in production, so I'm going to show you here.

VBScript has a fun way of interpreting include references that are nested inside an if statement. It will load all include references. If you have different functions, with the same name, it will use the second one.

Example

myTest_inc1.asp:
<%
Response.Write "<br/>Function 1 loaded...<br/>"
function myFunc(int1, int2)
    Response.Write "<br/>Function 1 executing...<br/>"
    myFunc = int1 * int2
end function
%>

myTest_inc2.asp:
<%
Response.Write "<br/>Function 2 loaded...<br/>"
function myFunc(int1, int2)
    Response.Write "<br/>Function 2 executing...<br/>"
    myFunc = int1 + int2
end function
%>

myTest.asp:
<%
    if (true) then
        Response.Write "<br/>Case 1 executing...<br/>"
%>
<!-- #include file="myTest_inc1.asp" -->
<%
    else
        Response.Write "<br/>Case 2 executing...<br/>"
%>
<!-- #include file="myTest_inc2.asp" -->
<%
    end if
    Response.Write myFunc(3,3)
%>


The output of myTest.asp is:
Case 1 executing...

Function 1 loaded...

Function 2 executing...
6





Thanks for reading.

No comments:

Post a Comment