jahlife
asked on
Executing Javascript in Ajax WITHOUT 'Eval'
I have posted two functions, A & B. Function A works fine in Ajax situation, as long as there is no SCRIPT block that needs to be executed. Function B works fine in Ajax situation and will execute a SCRIPT block.
I need to identify a solution for upgrading Function B to execute SCRIPT blocks without using 'eval'.
No usage of 'eval', that is the requirement.
Thanks so much for your input in advance.
I need to identify a solution for upgrading Function B to execute SCRIPT blocks without using 'eval'.
No usage of 'eval', that is the requirement.
Thanks so much for your input in advance.
FUNCTION A:
function setDocSectionArticleReview(url,id) {
var callback = function(result) {
document.getElementById(id).innerHTML = result;
}
processArticleCategoryReview(url, callback);
}
FUNCTION B (with eval to exec script):
function setDocSectionArticleReview(url,id) {
var callback = function(result) {
var divId = document.getElementById(id);
divId.innerHTML = result;
var x = divId.getElementsByTagName("script");
for(var i=0;i<x.length;i++)
{
eval(x[i].text);
}
}
processArticleCategoryReview(url, callback);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you wrap some summary description around your solution? You retrieve the"head" and then append script blocks into the head? Is that how they are being executed, just by adding them into the DOM at head block? My understanding is lacking... for the code sniplet below:
var s = document.createElement("sc ript");
s.type="text/javascript";
h.appendChild(s);
s.text=x[i].text;
Anyway, I am very thankful for your solution and that you took the time to provide your solution within my own function starting point. Very easy for me to test. I will pursue understanding it more.
Thanks again.
var s = document.createElement("sc
s.type="text/javascript";
h.appendChild(s);
s.text=x[i].text;
Anyway, I am very thankful for your solution and that you took the time to provide your solution within my own function starting point. Very easy for me to test. I will pursue understanding it more.
Thanks again.
ASKER
Can you wrap some summary description around your solution? You retrieve the"head" and then append script blocks into the head? Is that how they are being executed, just by adding them into the DOM at head block? My understanding is lacking... for the code sniplet below:
var s = document.createElement("sc ript");
s.type="text/javascript";
h.appendChild(s);
s.text=x[i].text;
Anyway, I am very thankful for your solution and that you took the time to provide your solution within my own function starting point. Very easy for me to test. I will pursue understanding it more.
Thanks again.
var s = document.createElement("sc
s.type="text/javascript";
h.appendChild(s);
s.text=x[i].text;
Anyway, I am very thankful for your solution and that you took the time to provide your solution within my own function starting point. Very easy for me to test. I will pursue understanding it more.
Thanks again.
>>My understanding is lacking
No, your understanding is Dead on!
No, your understanding is Dead on!
>> Is that how they are being executed, just by adding them into the DOM at head block?
Yes, when you add script to the head, it is executed immediately (even before the current function has completed execution). For a simple demo, create a new blank page then call this function from the body's onload event:
Yes, when you add script to the head, it is executed immediately (even before the current function has completed execution). For a simple demo, create a new blank page then call this function from the body's onload event:
function initPage(){
var h = document.getElementsByTagName("head")[0];
var s = document.createElement("script");
s.type="text/javascript";
h.appendChild(s);
s.text = "alert('This script was added automatically!');";
alert("in initPage");
};
ASKER
Beautiful.
Hey Hielo, can you please look at this: https://www.experts-exchange.com/questions/24054941/Javascript-ajax-confliction.html ?
Thanks
Thanks
http://24ways.org/2005/dont-be-eval
http://www.ajaxlines.com/ajax/stuff/article/using_json_in_ajax_without_using_eval.php
http://discuss.joelonsoftware.com/default.asp?design.4.255130.12
hope that helped you
waiting for your reply