Ok, I feel a bit dumb having to ask this. There are any number of hits on similar questions, but I can't seem to get it right. If I modify the DOM via jQuery/Ajax, when is a loaded script actually parsed/executed?
The code below works on F/F, but not on Chrome or Opera. 'Working' means that it executes 'do_init()' without error. The loaded script (actually an svg file; 'file.svg') defines a number of vars which are required by 'do_init()' (which is in the 'static' script). These vars are visible to F/F, but not to Chrome or Opera ("variable xxx is undefined"). The file/script is correctly loaded into the DOM in all 3 cases, and contains an <svg>
, with a <script>
inside it.
I can get this to work (the svg displays) in Opera (or break in F/F) by re-arranging code, but not in Chrome. Using .success and .complete makes no difference.
Thanks.
<head>
...
<script type="text/javascript" src="do_init.js"></script>
<script type="text/javascript"><![CDATA[
jQuery(document).ready(function() {
...
$("#submit").click(function(e){
e.preventDefault();
var jqxhr =
$("#svg").html(ajax_load).load("file.svg", function(response, status, xhr) {
if(status == "error") {
var msg = "Error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
} else {
do_init(); // Ok in F/F, not in Chrome/Opera
}
});
});
});
]]></script>
</head>
<body>
<button id="submit" type="button">Click Me!</button>
<div id="svg"></div>
</body>
EDIT 1
It turns out that they're never executed - at least in IE9, FF8, Chrome, Opera, and Safari. They are executed in FF7. I've just written a minimal test page which does an Ajax load of a script which is just an alert. This only shows an alert in FF7. I've also tried the script wrapped in an <svg>
, which makes no difference.