I’ve been using jquery 1.3.2 to pull snippets of html (including script) from a server. A typical response might be:
<div id="content"><div id="inner">...
<script type=...> alert("hello world");</script>
<p>Hello World</p>
</div></div>
I’ve been using the query .get function:
$.get($(this).attr("href"), function(response) {
$("#inner").replaceWith($("#inner", response));
});
And everything is fine and works as expected: the returned html snippets get loaded into the DOM and the scripts run.
When I use 1.4.2 however, I notice that the script tags have been removed and no longer run.
Stepping into the newer jquery codebase yields the lines of code (line 4498) :
ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
which seems to be the culprit ‘removing’ the script from its happy resting place as part of the selector process. But it doesn’t help me in injecting exactly what i want into the DOM.
Any idea why jquery is doing this? More importantly, how can I go about fixing this so that my scripts run?
If you can change your server side code to return this (just remove the wrapper divs which are present anyway)
then you could use
Which doesn’t seem to suffer from this problem.
Or if really just a snippet of this form (which is valid xml) is returned, you might also try specifying
xmlasdataType.