Solved

Problem executing document.write() function via ajax

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

732 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