Using an ASPX file as a Script Source

  • Thread starter Thread starter Moskie
  • Start date Start date
M

Moskie

I recently came across an application where the 'src' attribute of a
script tag was an ASPX file. Like so:

<script language="JavaScript" src="scriptfile.aspx"></script>

This is a new idea for me, and I had some questions about it.

When scriptfile.aspx is processed, is it going to have access to the
same request object and submitted form variables that the parent page
has? Will that aspx page be able to access the public and protected
members with <% %> server tags as the parent page would?

And maybe someone can link me to a guide on the web that details what
can and can't be done in this situation.

Thanks.
 
If you are going to emit Javascript from an ASPX page, you will need to
remove everything in the ASPX (Html page) portion except the <@page
declaration at the top.
In your Page_Load EventHandler, that is where you would
Response.Write the string of script.
 
Hi,
If you are going to emit Javascript from an ASPX page, you will need to
remove everything in the ASPX (Html page) portion except the <@page
declaration at the top.
In your Page_Load EventHandler, that is where you would
Response.Write the string of script.

And also set the Response.ContentType to "text/javascript" ?

HTH,
Laurent
 
That much I understand.

My questions are pertaining to what I actually have access to when that
aspx page is processing (i.e. in the Page_Load handler). Do I have
access to the same Requset object as the parent page (the page with the
script tag)? Or is this going to be a whole new request with a whole
new request object?

My gut tells me that this is going to be whole new request, and I have
no way to "pass" anything to it when it is being requested via a script
tag.... unless ASP.Net is doing something weird that I'm not aware of.
 
My gut tells me that this is going to be whole new request, and I have
no way to "pass" anything to it when it is being requested via a script
tag.... unless ASP.Net is doing something weird that I'm not aware of.


Your gut is right - the browser sends a new request to get the
javascript file. You *could* store bits in session and process them when
the request comes back in but this seems like a horrible kludge to me,
 
Moskie wrote:

My gut tells me that this is going to be whole new request, and I have
no way to "pass" anything to it when it is being requested via a script
tag.... unless ASP.Net is doing something weird that I'm not aware of.

Why not use a query string to "pass" parameters to the ASPX page
generating the script file?

<script type="text/javascript"
src="scriptfile.aspx?param1=value1&param2=value2"></script>

HTH,
Laurent
 
Back
Top