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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.write

Status
Not open for further replies.

ccddmm

Programmer
Mar 23, 2006
7
US
I have a question regarding document.write.

In the following code segment, there are two document.write. Does the 2nd document.write get exectued after the image mypic.jpg is completely downloaded by the client? Or not?

////////////////////////////////////////////////////////////

<html>

<script language"javascripte">

document.write('<img src="MYPIC.JPG" ' + 'WIDTH=10 HEIGHT=10 >');

document.write("hello");

</script>

<body>
here.
</body>
</html>

 
No, it should not wait.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
So is there any way to make sure the image is downloaded and then execute another statement.
 
Execute another document.write? no

Execute a javascript function call? yes

After the page has loaded any subsequent document.write calls will overwrite the rest of the page with the new document.write material. It's like copying a file to a directory where the file already exists, and overwriting the old file.

If this is a situation where you need text to be displayed to the screen then you have other options that don't include document.write. Here's an example:
Code:
<html>
<script type="text/javascript">

function blah() {
   document.getElementById("testDiv").[!]innerHTML[/!] = "hello.";
}

</script>

<body>
<img src="MYPIC.JPG" width="10" height="10" [!]onload="blah()"[/!] />
<div id="testDiv"></div>
</body>
</html>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Kaht:

Thanks for your reply.

one more question:

If I put a 30kb plain text in the document.write
document.write ("<!-- This is a 30KB text...... -->");

document.write ("here");

Will the 2nd statement also get exectued no matter the 30KB completes downloaded?






 
I would guess yes.... I never use document.write (and I'm willing to bet most of the MVPs here probably don't either) so I'm not really sure of all the timing issues associated with using it. The only thing I can say is try it out and see what happens.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
I did a test.

document.write (" This is a 30KB text...... ");

document.write ("here");

What I saw was that the whole block of 30KB text is written and the "here" is appended. So I guess the 2nd one executes after the 1st one finishs. But again since the connection speed is too high on my local box, I am not sure about the test result.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top