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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script - source = http://xxxx - Can you get remote script? 1

Status
Not open for further replies.

DarwinIT

Programmer
Apr 25, 2008
142
US
I ran into a situation where we need to include some javascript in our page. The code simply is this:

<script src='<noscript><img src=' border='0' width='1' height='1' alt='' /></noscript>

That process is recording tracking information so it has to run some code and then return something to the calling page.

Can you remotely generate actual javascript in this situation (which could make it dynamic based on parameters sent in to the source).
How would you return javascript from a called page? I tried using response.write and I got the javascript plus an entire html page behind it. I tried Server.Execute which is what we do with a gif file we return from a called page but that errros out.
 
you mean you just want to return a snippet of markup to the server? like the source would return
Code:
<script> my javascript goes here</script>
If that's the case, and you are using webforms, I would recommend a simple generic handler (ashx)
Code:
public class MyJsHandler : IHttpHandler
{
   public void Process(HttpContext context)
   {
       var js = //dynamically create javascript;
       context.Response.Write(js);
   }

   public bool IsReusable { get { return false; } }
}
from the markup you simply reference
<script scr="MyJsHandler.ashx?optionalParameters..."></script>

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 

Ahhh. SO instead of using an aspx page I need to use ASHX?
I saw that extension the other day for the first time and wondered what that was. So do I choose to add a Generic Handler in a regular Web Project?
Thanks!!!
 
So do I choose to add a Generic Handler in a regular Web Project?
yes.

a little more information about aspx vs ashx. aspx is a webform extension. both implement IHttpHandler. aspx does alot more work to setup the form, viewstate, web controls, and ultimately renders the html.

ashx (generic handlers) are just that a blank slate to have full control over the response stream. this is one of the key asp.net (not weforms) extensibility points that allows for other html frameworks like MSMVC and Castle's Monorail.

another option is to simply create a class to inherit IHttpModule and register this class in the web.config under the system.web handlers node.
Code:
public class MyClass : IHttpHandler
{
    //implement interface
}
Code:
<system.web>
  <HttpHandlers>
      <add Verbs="*" type="The.Name.Space.MyClass, AssemblyName" />
  </HttpHandlers>
</system.web>

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Wow. Thanks for the tutorial!! This is great info!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top