Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.write() misbehaving (?) 1

Status
Not open for further replies.

dgriffin

Programmer
May 26, 2001
50
0
0
US
I have only dabbled in Javascript. I cobbled this code together from articles I have read on line and got it working with the exception of the "document.write()" enclosed below. All other document.writes in this web page work fine, but I do not understand why this one misbehaves. It fires, so the logic is working, but once the entire page has displayed properly, the page is instantly overwritten with a blank white page with the contents of the document.write in the upper left corner and the browser churns endlessly (not hung) with a "waiting for host" message in the footer.

Obviously there is something about this XMLHttpRequest() environment that I don't know about. How do I get the document.write() function to write the text into the table cell inline, conditionally?

Code:
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH=100% BORDER=0>
<TR>
   <TD>
   <SCRIPT LANGUAGE="JavaScript">
   <!-- hide script
   if (!xmlHttp==false) {
      xmlHttp.open("HEAD","AfricanChickenWings.html",true);
      xmlHttp.onreadystatechange=function() {
         if (xmlHttp.readyState==0 || xmlHttp.readyState==4) {
            var dPage = new Date(xmlHttp.getResponseHeader("Last-Modified"));
            dPage = dPage.getTime();
            if ((dToday -dPage) /1000 /60 /60 /24 <90)
               {
               document.write('<FONT SIZE="-2" COLOR="#FF0000">New</FONT>');
               }
         }
      }
      xmlHttp.send(null)
   }
   // hide script End -->
   </SCRIPT>
   </TD>
</TR>
</TABLE>

 
Hi

Avoid using [tt]document.write()[/tt] as possible.

If the rendering of current [tt]document[/tt] was finished, the [tt]document[/tt] is closed. If you call [tt]document.write()[/tt] for a closed [tt]document[/tt] it will issue an implicit [tt]document.open()[/tt] first. And that will discard the current [tt]document[/tt] and create a new one.

In your code [tt]document.write()[/tt] is called in an asynchronous AJAX request's callback function. That is executed in separate thread, so the [tt]document[/tt]'s rendering is most probably finished before the [tt]document.write()[/tt] is executed.

Either use DOM methods to insert that 'New' text into the [tt]document[/tt] or use synchronous AJAX request. The first solution should be preferred.


Feherke.
 
D'oh! Thanks feherke. That was just the elbow jab I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top