jQuery .load() / .ajax() not executing javascript in returned HTML after appended

When load() is used with a fragment selector the script element will be stripped out before the fragment is added thus the script will not get executed.

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:

So Try

$.get('partial.html', function(result){
    $result = $(result);

    $result.find('#content').appendTo('#new_content');
    $result.find('script').appendTo('#new_content');
}, 'html');

Demo: Fiddle

Leave a Comment