generating a .js file with asp.net

W

WL

Hi,

I'm trying to build an .ASPX file that returns the contents of a .js file,
so that I can use:

<script language="JavaScript" src="/mypage.aspx"
type="text/javascript"></script>

Even when disabling viewstate etc I still end up with ASP.NET returning
contents as set with
Response.Write("var test=12;") in the PageOnLoad I end up with this extra
HTML code:

Thanks,

Wim

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>page4</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form method="post" action="page4.aspx" id="Form1">
<div>
<input type="hidden" name="__VIEWSTATE"
value="/wEPDwUKLTUxMTcwNzgxMGRkPbcvOzDy6UopR2KGJnAsry/0lgY=" />
</div>

</form>
</body>
</HTML>
 
M

Marina

2 ways to get rid of that, that I can think of:

1) Remove all that stuff from the .aspx doing the generation. So leave only
the @Page directive. Since all that stuff is there in the .aspx, it
automatically becomes part of the response.
2) Try a Response.Clear before your .aspx starts Response.Write'ing its
javascript contents.
 
W

WL

Response.Clear does not work because the ASP.NET stuff is added AFTERWARDs.
Meanwhile I found I out you can do this in the page.obload, after having set
the response.content:

Response.End()

Wim
 
M

Marina

Ok yes, good point, you would not need to clear what was already there - but
only make sure that nothing else got written out.
 
W

WL

Marina,

I just found it: how stupid of me: really your option 1 is the solution. I
was so much under the impression of Dotnet generating all kind of code for
me automatically that I forgot that the code I wanted to get rid of was just
inside my .aspx file.

My sincere apologies for my stupidity ;-)

Wim
 
S

Shawn B.

I do this frequently in my code. In fact, I dynamically generate my
includes and reference them that way.

What you do is clear everything in your aspx file except the page directive.
Then, in the code behind, add the following line:

Response.AddHeader("text/javascript");
.... // emit the script
Response.End();


That should do.

Thanks,
Shawn
http://www.zenofdotnet.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top