Do you *generate* the HTML dynamically, or is it a static file?

If you generate it dynamically on your server, you can wrap the script
inside a function whose name comes from the query string. Then in
JavaScript, each time you start an ajax download, generate a unique function
name and pass that name up to the server in the query string. Then in the
success callback, call that function and pass it an argument with your
context.

This is similar to the JSONP technique used in my old JSON plugin [1] and
other similar JSONP implementations.

I'm not sure what to suggest if the HTML is a static file.

-Mike

[1] http://mg.to/2006/01/25/json-for-jquery

> From: Ryan
> 
> I have the following file loaded by calling $.ajax
> 
> <div>This html is loaded dynamically
> <script>
>     alert('Script executed from the loaded sample'); </script> </div>
> 
> The script executes fine and the callback from $.ajax works. 
> My issue is that I would like the loaded script to attach to 
> the object that loaded it. I modified the callback as follows;
> 
>     var self = this;
>     $.ajax({url: urlToLoad, dataType: 'html', success: 
> function(data) { self.ContentReceived(data,self); } });
> 
> Which means in my callback I can find the object that it 
> relates to, but the callback occurs after the script has been 
> executed.
> 
> Does anyone know a safe way of injecting context into the 
> loaded script? I could somehow set a variable that is picked 
> up by the loaded script but then I might have issues with 
> multiple divs loading and resetting this variable. 
> Alternatively if the script exists within the dom I might be 
> able to shimmy up the hierarchy and find the div that links 
> to my object. But I'm not sure if this is even possible (I 
> think the script is executed globally, not within a DOM container).

Reply via email to