One step back, two steps forward:
document.write
is one of a set of three functions
document.open()
opens a document for writing from scratch, deleting existing document content if there is any.
document.write( string)
inserts a string into the character stream used to construct the web page.
document.close()
closes a document for writing. Any further writes will re-open the document and wipe out existing content in the process.
Now take into consideration
- At the end of the page input stream, the document is automatically closed.
- Documents are constructed using a "Document Object Model" ("DOM") which can be accessed and manipulated from within script.
document.open/write/close
originated before the DOM was standardized and became available for use.
So document.write
has little use in modern web programming. It wipes the page out if used after a page has finished loading. It is almost entirely restricted to tutorials for students who haven't learned the DOM exists yet, and occasionally when programmatically writing the content of a child window opened with window.open
.
All the HTML elements in a page exist as HTMLelement nodes in the DOM. These can be accessed by calling methods such as document.getElementById
or document.querySelector
and are returned as JavaScript object values. HTMLElements differ according to tag type, but if they represent an HTML container element, have properties such as innerHTML
and textContent
which, when updated with text strings in script, change the content of the rendered page.
In answer to your question, "demo" is the id value of an HTMLParagraphElement, where id values are used to access particular element in the DOM - id
values should be unique among page HTMLElements.
The (paragraph) element object is obtained by querying the DOM using document.getElementById
. Changing the elements innerHTML
content subsequently causes the document to be re-rendered with the new content, updating display of the page.