?
Solved

Problem executing document.write() function via ajax

Posted on 2008-10-23
4
Medium Priority
?
1,896 Views
Last Modified: 2012-05-05
Hi all,

I am having an issue with my ajax returning value generated by javascript. Understand that when Ajax calling a file (lets say .php) which contains of javascript, the return value would be in plain text (which means that the <script> will not be running) so that I have created the code as attached in snippet to replace <script> with <script type='text/javascript'> to force the javascript function to run properly.

However right now I am having issue of returning value from javascript using document.write() to the divId.innerHTML in my mainpage. What I suspect is that document.write() will overwrite my entire main page rather than only the divId.innerHTML so when ajax returning the document.write(), it stops the operation - which problem that I am having right now.

FYI: I am able to use any function in javascript properly (such as alert() ), but not document.write(). I tried to use document.getElementById(divId).innerHTML = 'theStringValue', it can run properly, but due to a reason that I need to call another function which force me to use document.write so I drop this idea by using document.getElementById.

This issue has struggled me for the past 24 hours. Your help would be very much appreciated.

Thanks
function setAndExecute(id, result){  
	var divId = document.getElementById(id);
	divId.innerHTML = result;
	var x = divId.getElementsByTagName("script");
	var h = document.getElementsByTagName("body")[0];
	for(var i=0;i<x.length;i++)
	{
		var s = document.createElement("script");
		s.type="text/javascript";
		h.appendChild(s);
		s.text=x[i].text;
	}
	alert (s.text);
}

Open in new window

0
Comment
Question by:encus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 22793391
Try to redefine the function before the AJAX call, then from inside that function you can call multiple functions (if you want)

document.write = function(text) {
  alert(text); // or whatever
}
0
 

Author Comment

by:encus
ID: 22793635
Hi xyp, I tried however it does not work also, it seems like I found out that people also unable to solve this kind of situation until they use iFrame.

No idea if there is a way to pass value from document.write() to a specific id.innerHTML via ajax
0
 
LVL 13

Accepted Solution

by:
Xyptilon2 earned 2000 total points
ID: 22793722
I think this is what you're looking for: http://www.webdeveloper.com/forum/showthread.php?t=138830
0
 

Author Comment

by:encus
ID: 22794258
Hi Xyp,

Yes this is what I am looking for, appreciate it very much. However I am having a problem right now as it runs the entire script and document.write() has replaced entire page of the MainPage instead of the id.innerHTML. So i think I would go for frame instead then.

Anyway thank you so much for your assistance.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question