On php, I have array which had 2 parts as following
array('threeRowHtml'=> '<HTML output string>',
'jsCode'=> '<script output string>'
)
I return that array for my ajax call. I parsed the response and I could take two of those strings. I would like insert them into my page and eval to execute the return js script. I could insert "threeRowsHtml" OK, but with 'jsCode' did not work.
$.ajax({
url: postUrl,
type: "post",
beforeSend:function(){
process.show(); // just my popup
},
success: function(data){
data = $.parseJSON(data);
if(data.returnCode == 1){
//var jsCode = eval("(" + data.jsCode + ')');
console.log(data.jsCode); // print out OK
$('#rp_script').replaceWith(data.jsCode); // did not work
$('#rp_content').replaceWith(data.threeRowsHtml); // insert into page OK
}
else{
$('#rp_errors').replaceWith(data.errorHtml);
}
process.hide();
}
})
Here is the location for inserting the response
<div id="rp_content">
</div>
<div id="rp_actions">
</div>
<div id="rp_script">
</div>
<div id="rp_errors"></div>
The return script string's syntax was fine, because if it was wrong, it would notice me the JS error token. The result is always empty string for "jsCode" and I don't know what wrong with it. Any help appreciated!
I've found out the problem. jquery html() strips out script tags
Now I can print out the script into my page
$('#rp_script')[0].innerHTML = data.jsCode;
Of cause, I still use global.eval to execute my script.