[Solved] JQuery / JavaScript won’t work WITHIN AJAX script [closed]


Your code…

$("#content").load("content.html #about");

is like the example in the “Script Execution” section of the .load() documentation.

When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated, and thus are not executed. An example of both cases can be seen below:

in the following case, script blocks in the document being loaded into #b are stripped out and not executed:

$( "#b" ).load( "article.html #target" );


You could try using the jQuery .getScript() method within your .load() callback function. This will ensure the new HTML is loaded before the scripts are loaded and executed.

$("#content").load("content.html #about", function() {
    $.getScript("myJavaScript.js");
});

3

solved JQuery / JavaScript won’t work WITHIN AJAX script [closed]