简体   繁体   中英

ASP Classic - Include VBScript page in a JavaScript page?

Is there a way to include a VBScript page into an ASP page written using Javascript?

There is an ASP page written in Javascript, but our sidebar menu is written in VBScript. When I include the sidebar asp file into the Javascript the server gives an error.

< %@ LANGUAGE="JavaScript" %>

<%

...

< !--#include file="sidebar.asp"-->

...

where sidebar.asp is written using VBScript.

You can try this, but my guess is that the sidebar.asp will be executed before your Javascript code.

< %@ LANGUAGE="JavaScript" %>

<%

...
<script language="VBscript" runat=server> 
< !--#include file="sidebar.asp"-->
</script> 
...

I do this all the time, but I write my ASP/JScript pages a bit differently. Instead of switching the page language to "JavaScript", I leave it at the default "VBScript" and then use a <SCRIPT LANGUAGE="JavaScript" RUNAT="Server"> block for my JScript code. The JavaScript SCRIPT blocks are executed before the normal <% %> tags, so I do all my page processing in the SCRIPT blocks and then simply plug the results into the page with <% %> tags. Here's an example:

mainpage.asp:

<SCRIPT LANGUAGE="JavaScript" RUNAT="Server">
var name;
var address;
var phone;
function main() {
    var rec = go_to_database();
    name = rec.first_name + " " + rec.last_name;
    address = rec.address;
    phone = rec.phone;
}
</SCRIPT><% main() %>
<html><head><title><%= name %></title></head><body>
<p>Name: <%= name %><br/>
Address: <%= address %><br/>
Phone Number: <%= phone %></p>
<!--#include file="subpage.asp"-->
</body></html>

subpage.asp:

<p>Blah blah blah, some random VBScript code: <%
    Dim whatever
    whatever = some_silly_thing()
    Response.Write(whatever)
%>.</p>

So, first IIS processes the SSI and includes subpage.asp into mainpage.asp . Then, it evaluates the JScript SCRIPT block, declaring the variables name , address , and phone and defining the function main .

Then it evaluates each <% %> tag in order. <% main() %> call the main function and sets values for name , address , and phone . Then <%= name %> , <%= address %> , and <%= phone %> substitute those values into the page. Finally, the <% %> code from subpage.asp is evaluated and the Response.Write value ends up in the page output.

While the whole page is not written in JScript, the vast majority of the code can be, inside the SCRIPT block. Would that work for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM