Friday, 11 August 2017

javascript - document.write strange behavior








Title



My Web Page







output:



The tags in the page are:
HTML
HEAD
META
TITLE
SCRIPT
BODY
H1
B



By un-commenting the first document.write() I get the following output:




8The tags in the page are:
HTML
HEAD
BODY



Some tags are missing, I don't understand where the mistake is! I thank you in advance.


Answer



This is because document.write being called after the document is done loading, it erases and replaces the current document. If you see here: onload="showtags()" that's why it's happening. document.write needs to be called BEFORE the DOM is done loading. For example:






Is ok, because the DOM is still loading. But in a onload, or after the page is done loading in general , is not.



No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...