I'm not sure this question has the best of titles but I'm not sure what else to call it so sorry for that.
I'm using ajax to pull in content for a div on my website (after an option is selected). The content is a form generated by a PHP script. When the form is submitted a JavaScript function should be called but I'm just getting an error that says the function can't be found.
The JavaScript is pulled in via ajax with the form and I can't really change that as it needs to change demanding on the option selected.
My question is should this work? if not I'll just have to re think the way I'm doing it, just wanted to check if it wasn't working because it never will or if I'm doing something wrong.
I would show the code but it's very long.
Thanks in advance!
Edit: thanks for all the comments ect, apologies for not including the code before here it is.
function select(id){
$.ajax({
url: 'select/'+id,
type: 'GET',
dataType: 'html',
success: function(msg) {
$('.product_details').html(msg);
return false;
}
});
}
-
3Just show the relevant part of the code (the Ajax call, for ex.). Or use pastebin or codepad. Are you using any framework (jQuery, Dojo, Mootols...) for the Ajax part? in jQuery you can look into delegate() or on() (> 1.7) maybe, but it's difficult to tell without seeing anythingDamien Pirsy– Damien Pirsy2011年11月23日 16:23:12 +00:00Commented Nov 23, 2011 at 16:23
-
I'd presume that adding a script element via "append()" or "html()" doesn't actually execute it, meaning that included functions won't be declared. It seems strange that your code has to change based on user choicesRoman– Roman2011年11月23日 16:29:42 +00:00Commented Nov 23, 2011 at 16:29
-
3What you want to do is possible, but we really can't help you unless we see what you're doing.gen_Eric– gen_Eric2011年11月23日 16:30:53 +00:00Commented Nov 23, 2011 at 16:30
-
The most important thing with your problem is you have to "evaluate" the js code you are inserting into the page with ajax. The examples (at least the MooTools one) given by @TimWickstrom.com should help you with it.Nicolás Ozimica– Nicolás Ozimica2011年11月23日 20:31:54 +00:00Commented Nov 23, 2011 at 20:31
-
Should be working like a champ, do you have a link?Tim Wickstrom– Tim Wickstrom2011年11月28日 20:22:46 +00:00Commented Nov 28, 2011 at 20:22
1 Answer 1
Are you using a javascript library?
With jQuery specify a data type of html and make sure the script tags are before the HTML in the response
$.ajax({
url: "something.php",
dataType: "html",
success: function(data, text, request) {
...
}
});
in mootools...
var myRequest = new Request({
url: "something.php",
evalScripts: true,
onSuccess: function(responseText, responseXML){
....
}
});
myRequest.send();
Now your passed tags will be evaluated and available to the DOM