javascript - Render any response from ajax call (HTML and/or JS) -


i wonder what's effective way render might come ajax call. response can html and/or javascript

i doing document.write() since ajax call sync. have use async calls can't write page using document.write, have add content specific html object when call finished.

i tried this

   $jquery.ajax({    url: "myurl.ashx",    data: params,    success: function(data){         var newdiv = document.createelement('div');         newdiv.innerhtml = data;         document.getelementbyid('target').appendchild(newdiv);    }    

but doesn't work javascript returned, broken (this means; doesn't render content or weird things showing content in new tab). work simple scenarios.

i wonder if there better approach works better this.

thanks!

edit

this code, example, doesn't work expected. renders content on blank page instead of adding content current page. i'm testing ff 3.6. simplicity, i'm harcoding javscript on str var, script returned server. content returned server ads, content dynamic , totally random.

        var str = "<sc" + "ript language=\"javascript\" type=\"text/javascript\">";         str += "document.write('<scr'+ 'ipt language=\"javascript\" src=\"anything.js\" type=\"text/javascript\"><\/scr' + 'ipt>');";         str += "<\/script>";         var newdiv = document.createelement('div');         newdiv.innerhtml = str;         document.getelementbyid('target').appendchild(newdiv);     

well jquery equivalent of have there be:

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

you have explain mean

doesn't work javascript returned

for better answer.

if there <script> tags within data jquery evaluate them in global scope if use jquery.append function demonstrated above.

update

just remove document.write statement. use document.write when page still loading.

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

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -