I am using the jQuery .load function to insert some Javascript into my page but I cann开发者_如何学运维ot get it to execute. Has anyone any ideas?
The load:
$("#seniors").click(function(){
$("#swfobject").load("ajax/swfobject.php #swfobject", {gallery: "14"}, function() {
flash();
}); });
Contents of swfobject.php:
<?php $gallery = $_POST['gallery']; ?>
<div id="swfobject">
<script type="text/javascript">
var flashvars = {
xmlfile: "http://foo.com/API/xml.php?option=<?php echo $gallery ?>",
xmlfiletype: "Director"
}
var params = {
base: ".",
menu: "false"
}
params.wmode = "opaque";
var attributes = {}
swfobject.embedSWF("SSP-AS3.swf", "gallery", "958", "530", "9.0.0", false, flashvars, params, attributes);
function flash() {
alert('flash has run.');
}
</script>
</div>
Per the jQuery documentation, the callback is invoked after the response is received and the content is added to the innerHTML of the matched element. That doesn't guarantee that the browser's JavaScript interpreter has interpreted any scripts in the content.
You might consider something like this:
var loadHandler = function(){
if(flash){
flash();
} else {
setTimeout(loadHandler, 10);
}
}
$("#seniors").click(function(){
$("#swfobject").load("ajax/swfobject.php #swfobject", {gallery: "14"}, loadHandler);
});
That will test whether the flash function exists, and if not wait 10ms, repeating that until the script is in fact interpreted.
精彩评论