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


i've tried using .load() , $.ajax html needs appended container, javascript within returned html not being executed when append element.

using .load():

$('#new_content').load(url+' #content > *',function() {     alert('returned'); }); 

i've tried switching $.ajax call. extracted script returned html after appended container, same problem, js isn't being executed or appended in case, , understand it, trying append <script> dom element frowned upon?

using $.ajax:

$.ajax({ url: url }).done(function(response) {     var source  = $("<div>").html(response).find('#overview_script').html();     var content = $("<div>").html(response).find('#content').html();      $('#new_content').html(content);     $('<script>').appendto('#new_content').text(source).html();  }); 

ideally run javascript in callback after have appended html, variables being set values returned controller.

so in summary, trying html contains js needs run after html has been appended, have had no luck using .load() or $.ajax() script in returned html either disappears upon appending it, or not execute @ all.

when load() used fragment selector script element stripped out before fragment added script not executed.

when calling .load() using url without suffixed selector expression, content passed .html() prior scripts being removed. executes script blocks before discarded. if .load() called selector expression appended url, however, scripts stripped out prior dom being updated, , not executed. example of both cases can 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


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -