I'm returning html with a script tag in it from a jquery ajax call. How can I execute the contents of the script tag?
views:
72answers:
2
A:
what exactly are you trying to achieve? this sounds like it could lead to bad places as it stands currently. (By bad places I mean XSS type exploits)
dhoss
2010-04-13 02:59:16
Basically I'm returning some html and need to initialize a few events using jquery (ie. a mouseover or click event).
devlife
2010-04-13 03:05:55
my question is - why does it have to be initialized via the returned data? You could set up a jquery listener so that when the thing being clicked is in fact clicked, the data (not the javascript, you'd be returning the raw data) is pulled out and said javascript bit is initialized *with* that data, if that makes sense.
dhoss
2010-04-13 03:12:33
Right that's what I'm saying. I need to setup a listener on the element(s) being returned. The issue is that I'd rather not have to go back to all of my ajax calls and set up the initialization. I'd prefer to have the result be self contained.
devlife
2010-04-13 03:19:49
+1
A:
If the script had a function in it, you could call the function (i think). I think the browser may block this sort of activity to prevent cross site scripting attacks.
Jeff Martin
2010-04-13 03:00:57
Let me give you an example (and why I would prefer not to do it this way).I have a bunch of partial views (using MVC) which each utilize another partial view. The sub-partial view will be rendered in a bunch of different places and I'd prefer not to have to add a bunch of calls to this javascript function every time I load a partial view via ajax. If there is any way that the script tag inside the sub-partial view can be executed without an explicit call that would really be ideal.
devlife
2010-04-13 03:12:09
I'm not sure I understand from your example why having script in being executed from a partial view loaded by ajax is different from just calling a JS function after the ajax is loaded. I have an MVC project where I do some partial view refreshing and I have the main view load all the js, and the functions just get called after the ajax call. You are already explicitly calling the function that does the ajax, right?
Jeff Martin
2010-04-13 15:25:18