Link to home
Start Free TrialLog in
Avatar of jahlife
jahlifeFlag for United States of America

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.

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);
}

Open in new window

Avatar of Smart_Man
Smart_Man
Flag of Egypt image

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jahlife

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("script");
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.
Avatar of jahlife

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("script");
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!
>> 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:
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");
};

Open in new window

Avatar of jahlife

ASKER

Beautiful.