Why document.write () will empty the original content

Source: Internet
Author: User

Why document.write () will empty the original content:

Many friends may have encountered this situation, that is, when using the document.write () function to write content to the Web page, the original content in the document will be emptied, which is a problem for beginners, the following is a brief introduction to why this situation, Of course, we know how to avoid this kind of situation.
Let's look at a code example:

<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.softwhy.com/" /> <title>Ant Tribe</title> <Scripttype= "Text/javascript">window.onload=function() {document.write ("Sharing Mutual Assistance");}</Script> </Head> <Body> <Div>Ant Tribe welcomes You</Div> </Body> </HTML>

From the above code can be seen document.write () function to empty the original document content, the following describes the reasons for this situation:
The Window.onload event is that when the document content is fully loaded and then the event handler is executed, the flow of the document is closed, and the execution of the Doucment.writ () function automatically calls the Document.open () function to create a new document flow. and write the new content, and then through the browser display, so that will overwrite the original content. But many friends still have this question, why like the following situation, the original page content will not be overwritten, the code is as follows:

<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.softwhy.com/" /> <title>Ant Tribe</title> <Scripttype= "Text/javascript">document.write ("Sharing Mutual Assistance");</Script> </Head> <Body> <Div>Ant Tribe welcomes You</Div> </Body> </HTML>

In the above code, the original document content is not emptied, because the current document flow is created by the browser, and the Document.wirte () function is in it, that is, when the function is executed, the document flow is not closed, this time does not call Document.open () function to create a new document flow, so it will not be overwritten. There may be friends who ask why the following method is still not working, the code is as follows:

<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.softwhy.com/" /> <title>Ant Tribe</title> <Scripttype= "Text/javascript">document.close (); document.write ("Sharing Mutual Assistance");</Script> </Head> <Body> <Div>Ant Tribe welcomes You</Div> </Body> </HTML>

Above using Document.close () to close the document flow, why still cannot overwrite the original content, unfortunately, the document flow is created by the browser, no permissions to manually close, Document.close () function can only be closed by Document.open () The document flow created by the function. Look at the following code example:

<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.softwhy.com/" /> <title>Ant Tribe</title> <Scripttype= "Text/javascript"> functionCreate () {varNewWindow=window.open ("","Ant Tribe","_blank"); NewWindow.document.write ("Ant Tribe welcomes you"); NewWindow.document.close (); NewWindow.document.write ("ABC"); } window.onload=function(){ varOBT=document.getElementById ("BT"); Obt.onclick=function() {Create ();}} </Script> </Head> <Body> <DivID= "Print">Ant Tribe welcome you, only hard struggle will have a better tomorrow.</Div> <inputtype= "button"ID= "BT"value= "View Effect"/> </Body> </HTML>

In the above code, the document flow created by Doucment.open () can be closed by the document.close () function, then the contents of the first output will be overwritten by the content of the second document.write () output.

Excerpt from: http://www.softwhy.com/forum.php?mod=viewthread&tid=13582

Why document.write () will empty the original content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.