Solved

Problem executing document.write() function via ajax

Posted on 2008-10-23
4
1,880 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
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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

785 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