Solved

Problem executing document.write() function via ajax

Posted on 2008-10-23
4
1,874 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
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Xyptilon2
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
I think this is what you're looking for: http://www.webdeveloper.com/forum/showthread.php?t=138830
0
 

Author Comment

by:encus
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now