Home >Web Front-end >JS Tutorial >Some tips on using document.write_javascript skills

Some tips on using document.write_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:48:251203browse

I have been using the document.write() method to display data to the browser, and use it as Alert(). It seems that this method is overkill. Let’s talk about its main uses.

The document.write() method can be used in two ways:

1. During the page loading process, use a script to add new page content.

2. Use a delay script to create the content of this window or a new window.

This method requires a string parameter, which is the HTML content written to the window or frame. These string parameters can be variables or expressions whose values ​​are strings, and the written content often includes HTML markup language. As shown in the following code, the educational system framework is loaded into the subpage

Copy code The code is as follows:


       
  
                                                                            >









Copy code

The code is as follows:


After loading the page, the browser output stream is automatically closed. After this, any document.write() method that operates on the current page will open a new output stream, which will clear the current page content (including any variables or values ​​​​from the source document). Therefore, if you want to replace the current page with HTML generated by a script, you must concatenate the HTML content and assign it to a variable, and use a document.write() method to complete the writing operation.

One more thing to note about the document.write() method is its related method document.close(). After the script finishes writing content to the window (whether it is this window or another window), the output stream must be closed. After the last document.write() method of the delay script, you must ensure that the document.close() method is included. Otherwise, images and forms cannot be displayed. Also, any subsequent calls to the document.write() method will only append the content to the page, but will not clear the existing content to write new values.

To demonstrate the document.write() method, we provide two versions of the same application. One writes to the document containing the script, and the other writes to a separate window.

Example 1 creates a button that assembles new HTML content for the document, including an HTML tag for the new document title and a color attribute for the tag.

In the example, there is an operator = which is unfamiliar to readers. It adds the string on the right side to the variable on the left side. This variable is used to store the string. This operator can easily convert several strings. Individual statements are combined into a long string. All new content can be written to the document using the document.write() statement combined with the content in the newContent variable, completely clearing the content in Example 1.

Then you need to call the document.close() statement to close the output stream. When the document is loaded and the button is clicked, notice that the document title in the browser title bar changes as a result. When you go back to the original document and click the button again, you can see that the dynamically written second page loads even faster than reloading the original document.

Example 1 uses document.write() in the current window.

Copy code The code is as follows:

Writing to Same Doc










Summary:

Recently I was writing a static resource loader, which used document.write. After going through a lot of troubles, I found that document.write still has some content, so I decided to play with some things and record them, and at the same time, it was accumulated for myself. Order something.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn