Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document.write method questions

I'm experimenting with write method & onload event. Here is my code:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body onload="document.write('body loaded!')">
        <h1>Hello World!</h1>
        <img onload="document.write('img loadeld!')" src="smiley.gif" alt="Smiley face" width="42" height="42" />
    </body>
</html>

If i run this in browser it outputs "img loadeld" and just "hangs", seems to be loading the page infinitely. I expected the browser outputs "img loadeld" and then as the body element is ready "body loaded" and just stops as normally.

My questions:

  1. Why is there such a hang? Why the onload event on img element blocks the browser from continuing & rendering "body loaded"?
  2. Why if i remove onload handler from img element the reponse is as expected - "body loaded" and the page isn't blocked.
like image 782
Mulligan Avatar asked Sep 11 '12 18:09

Mulligan


People also ask

What is document write method?

The document. write() method writes a string of text to a document stream opened by document. open() . Note: Because document. write() writes to the document stream, calling document.

Why is document write () not recommended anymore?

Another reason not to use document. write() is it doesn't support XHTML, but its not an issue since most web development uses HTML. Since document. write() fires after a page has finish loading, it causes performance issues and sometimes, may not even fire at all.

What is the use of write () method?

The write() method in HTML is used to write some content or JavaScript code in a Document. This method is mostly used for testing purpose. It is used to delete all the content from the HTML document and inserts the new content. It is also used to give the additional text to an output which is open by the document.

When should you use document write?

Possible situations to use document.write() It seems that the only “approved” time to use document. write() is for third party code to be included (such as ads or Google Analytics). Since document. write() is always available (mostly) it is a good choice for third party vendors to use it to add their scripts.


2 Answers

Simply put, calling document.write() after DOM ready causes it to overwrite the existing DOM.

like image 194
chrisfrancis27 Avatar answered Oct 12 '22 16:10

chrisfrancis27


Everything is working correctly (though not as you expected), and nothing is "hanging" or "blocking." It's all happening so fast, however, that it does not appear as such. Hopefully an explanation of the workings of writing to the document and the order of events will assist you:

Your IMG onload event fires after the document has been closed (document ready has been reached).

document.write(), however, can only output to an open document.

If a document has been closed, document.write() (docs) implicitly calls document.open() (docs) which wipes the entire document. Your call to write then outputs what you told it to. The document remains open until it is explicitly closed (document.close() (docs)), so the "loading spinner" continues to show.

The basic flow of operations, then, which is taking place (so quickly that you don't notice it all happening and things look broken) is:

  1. page request made
  2. page response received
  3. document is opened, content is parsed and put into place, including the first document.write (does not have to call open because document is currently open)
  4. document closes
  5. Retrieval for the IMG tag completes and the event fires
  6. event handler calls document.write
  7. document is implicitly re-opened (new doc created, all content lost; loading spinner displayed)
  8. your argument to document.write() is outputted into the new document
  9. everything is complete, document is still open

DOM manipulation techniques (appendChild(), writting to innerHTML, etc) should be used Instead of document.write in order to modify existing content without overwriting everything.

The good news in this is that since this is happening, you do know that your image is loading successfully. You just gotta work out the right way to react to it as I eluded to earlier.

like image 24
JAAulde Avatar answered Oct 12 '22 16:10

JAAulde