views:

25

answers:

1
<script type="text/javascript">    
$.get("url",function(data){
   $('#div').html(data);
});
</script>

after using this function data returns below:

<div id="info"></div>
<script type="text/javascript">
$.get("urlanother",function(data){
$('#info').html(data);
});
</script>

My problem is second get operation not work.

How to do it?

A: 

Setting jQuery html() or JavaScript innerHTML to a string with <script> in does not cause the script to be run. The script may get run later if you try to manipulate the DOM object representing it, but this is highly inconsistent across browsers and is behaviour best avoided. jQuery attempts to handle load()ed scripts in a consistent way, but fails.

So avoid inserting <script> into the document. Instead, keep your JavaScript code in static scripts loaded by the main document, and fire later on-content-loaded-from-get code using the callbacks jQuery gives you to let you know when they're done:

$('#div').load('url', function(){
    $('#info').load('urlanother');
});
bobince
My `data` variable maybe not return script. Sometimes return script sometimes return only html data.
ebattulga
Then you will have to return a JSON object that contains both an HTML snippet to add to the document and a separate JavaScript string to `eval()`. Though still, it's generally poor practice to be creating and running new code on the fly like this. It's much more stable and maintainable to keep your code static.
bobince

related questions