1

Is it possible to have an asp page return a javascript file dynamically in classic ASP? What I'm trying to do is return a specific javascript file to a calling asp page by passing through another asp page. The 'pass through' page will inspect a server variable and based on a setting with either return scriptA.js or scriptB.js to the calling asp page. The calling asp page needs to use the returned .js for navigation menus.

It's been over 7 years since I've done any Classic ASP, and got sucked into helping out on a project.

Any help is appreciated.

Thanks!

2
  • Are you looking to create the js file dynamically or just the inclusion of the scriptA vs. scriptB? More specifically, are scriptA and scriptB static on the file system? Commented Mar 29, 2011 at 18:11
  • The scripts already exist. I need to "choose" one of them based on a variable. I'd like to have IIS redirect any requests for the script directory to an asp page that will return the appropriate .js....if that makes sense. Commented Mar 29, 2011 at 18:41

4 Answers 4

3

You'd have to use Server.CreateObject("Scripting.FilesystemObject") to read the appropriate javascript file and use Response.Write to output the appropriate javascript file. Remember to set the content type to JavaScript.

Response.ContentType = "application/x-javascript"

Sign up to request clarification or add additional context in comments.

5 Comments

This is what I'm trying to do, but it doesn't seem to work. I think the problem is that I'm trying to redirect all requests to a folder with all of the .js files to a single .asp file that will do the Response.Write of the appropriate .js file. I'm trying this in IIS 5.1 with redirects on a folder. I am able to get the javascript to write out to a page, however, when I try to get the src from an asp page, all I get in firebug is: " Failed to load source for: myscript.js". I'm beginning to think that this is not possible to do.
@brutus35. It might be a case of firebug not being able to deal with the redirect when you try to open the .js file. I just a ran a quick test on IIS6 and IE. It worked like you are wanting it to. Maybe you could put a javascript alert to see if it is working?
I'm not getting any of the javascript back from the original calling page with <script src="/script/test/menu.js">. In IIS I have anything requesting a file from /script/test to get redirected to an asp page in a nother folder. That asp page is what I have doing the Response.Write of the javascript code, which I want the original page to be able to use....
Ha looks like you're correct. Seems to be working now, thanks for suggesting the alert. One more question, if I wanted to get the url of the request that forced the redirect, in this case <script src="/script/test/menu.js"> , how could I do that? I want to be able to tell what .js file was originally requested.
Ii was able to get this to work by setting the redirect variable to ?script=$V, then just grabbing the script name off of the querystring. Thanks for your help!
1

You can dynamically include files like this:

if Request.QueryString("param")="2" then  
    Server.Execute("page1.asp") 
else 
    Server.Execute("page2.asp") 
end if 

Where page1.asp and page2.asp have js files embedded in them (only).

Comments

1

Something like:

<script type="text/javascript" src=<%
if($Request->ServerVariables(URL)->item() eq 'here') { %>"something.js"<% }
else {  %>"somethingelse.js"<% }
%>></script>

Comments

0

Using JScript in Classic ASP

var curFileName = "";
if(Server("Variable") == 'true'){ curFileName = page1.js; }
else{ curFileName = page2.js; }

Response.Write("<script type='text/javascript src='" + curFileName + "'><\/script>");

Comments

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.