Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic external Javascript file

Status
Not open for further replies.

tcstom

Programmer
Joined
Aug 22, 2003
Messages
235
Location
GB
I want to use an ASPX page as an external Javascript file because I need a Javascript array containing database data and I want it external because this is an accessible application. So I'm including it like this:

Code:
<script src="../scripts/report_array.aspx" type="text/javascript"></script>

So I'm compiling a Javascript string in my Page_Load event, but my problem is that .NET automatically writes some default HTML into the output. Is there any way to prevent this happening? I esentially want to output a dynamic text file with no HTML tags...
 
What is the code being called? What is the output? What should the output be?
 
At the moment I'm just testing by writing a single Javascript variable initialisation into the file. So my Page_Load method just contains:

Code:
string strJavascript = "var testVariable = 'hello';";
Response.Write(strJavascript);

Which sends this to the browser:

Code:
var testVariable = 'hello';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
	<title>Test</title>
</HEAD>
<body>
	<form method="post" action="report_array.aspx" name="report_array">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTI3OTMzNDM4NDs7PhsqJ/hlt1nD3fipN/Z/f5CLjLN2" />

	
	</form>
</body>
</HEAD>

But all that HTML (which is generated automatically) causes the Javascript to error, so all I want it to send to the browser is this...

Code:
var testVariable = 'hello';

...so that I can include it as an external Javascript file as in my first post.
 
The problem is the way you are trying to write HTML to the page. Look up the Page.RegisterStartupScript and the Page.RegisterClientScriptBlock methods.

Jim
 
I'm aware of those methods but they are designed for writing Javascript into HTML. Any HTML tags in the output are going to prevent the page working when included as an external Javascript file.

A colleague has suggested creating an empty .js file and writing the Javascript to it upon every load of the page that needs to include it. That would probably work, but I'm sure there must be a way to prevent .NET writing default HTML tags...?
 
Ok, I guess I am confused on why you are trying to do this at all. An .aspx is an HTML page that is generated from the server, therefore, you will not be able to remove the default tags. You can include a .js file in your project if that will help you do what you want to do.

Jim
 
Yes, but the point is that the Javascript needs to be dynamically generated from a database so a static .js file is no good. But it sounds like I need to create the static file then update its contents dynamically on page load. That'll have to do. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top