1

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.

2 Answers 2

1

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> 
...
Sign up to request clarification or add additional context in comments.

1 Comment

This did not work for me. The first error I got was that I had to use the src attribute of the script tag. When I put the sidebar.asp as the src, then I got errors on the ASP content (the <% ).
1

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?

3 Comments

This is the best approach I've seen.
Thanks, I didn't know anyone was still writing Classic ASP!
Yup there's still a bit of legacy stuff out there. "If it ain't broken".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.