jquery load and javascript-Collection of common programming errors
I’ve seen many topics on this site about using jquery load and loading the script the in the html fragment. I, unfortunately, have not been able to get any of these methods (getscript) to work. Here is the code that loads the html and the supposed javascript.
$.ajaxSetup ({
cache: false
});
$(document).ready(function(){
$("#x").click(function() {
event.preventDefault();
$("#content").load("content.html", function(){
$.getScript("imagemod.js")});
return false;
});
});
When loading fragmented page loads, though, the pages javascript doesn’t seem to work. When I load the page not through ajax, the script seems to work. Here is the page that is suppose to have the javascript working but isnt. The page in question is reached by clicking on “large” of the american dogs menu. Help is much appreciated.
-
You are using an undefined variable ‘event’ and forgot a semicolon…
$.ajaxSetup ({ cache: false }); $(document).ready(function () { $("#x").click(function(event) { // event object is passed as first arg event.preventDefault(); $("#content").load("content.html", function () { $.getScript("imagemod.js"); }); // semicolon return false; }); });
Also be aware, that if you load an html document like
...
and not a fragment like
...
browser’s won’t properly import that into an existing document, because it’s a standalone DOM document. To do ajax and partial html, load a fragment like
...
.
Originally posted 2013-11-09 21:19:37.