Skip to content Skip to sidebar Skip to footer

Render Any Response From Ajax Call (html And/or Js)

I wonder what's the most effective way to render ANYTHING that might come from an ajax call. The response can be html and/or javascript I was doing it with document.write() since t

Solution 1:

Well the jQuery equivalent of what you have there would be:

success: function(data){
    $('<div />')
        .append(data)
        .appendTo('#target');
}

You will have to explain what you mean by

doesn't work for any javascript returned

for a better answer.

If there are any <script> tags within data jQuery will evaluate them in the global scope if you use the jQuery.append function as I demonstrated above.

Update

Just remove the document.write statement. Only use document.write when the page is still loading.

<scriptsrc="anything.js"type="text/javascript"></script>

Solution 2:

there are better ways to include js files than making an ajax call that loads javascript. I would look at .getScript()

Post a Comment for "Render Any Response From Ajax Call (html And/or Js)"