Solved

Problem executing document.write() function via ajax

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to hide html tags in text area 4 37
iframe detection of parent window scale 20 60
Angular - "nest" service calls? 18 24
replace all occurrences within a string 8 14
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now