Javascript Window Function guide-Writing content in a window

Source: Internet
Author: User
Tags blank page

Window. open () method to open a new window, document. open () method to open a new document, where you can use the write () or writeln () method to write content. Its syntax is:
Onewdoc = Document. Open (smimetype [, sreplace]);
Sminetype is a string that defines the MIME type. Navigator supports several MIME types, but Internet Explorer currently only supports "text/html ". The sminetype parameter is optional. The 2nd parameter is also a string that defines whether the new document to be written should replace the position of the current document in the history. If you want to achieve the replacement purpose, use the string "replace ".
"Replace" is basically used in a window with an empty document or "about: blank" URL. After "replace" is defined, the write () method can create HTML content in this window and replace the location of the current URL in the history. If "replace" is not defined, the created HTML has its own historical location. You can click the back button until it is blank.
Take a look at the following script Program Section:
VaR onewdoc = Document. Open ("text/html", "replace ");
VaR smarkup = "<HTML> Smarkup + = "<body> Hello, world! <Br> <a href0000'write.html '> return </a> </body> Onewdoc. Write (smarkup );
Onewdoc. Close ();
As you can see, the new document contains a link, so you can return to this page. If you click the back button of the browser, the browser returns to the previous page. Because the "replace" parameter is used, the new document (written document) replaces the position of the current document in the history, therefore, clicking the back button does not return to the current page (including the script page ). The buttons below execute the same script program, but there is no "replace" parameter. Therefore, you can click the back button of the browser to return to this page.
The following is the button Source code : <textarea id="runcode83555"><Br/> <p></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

As you can see in the above two examples, the last statement closes the output stream:
Onewdoc. Close ();
Usually, the document. Close () method closes the output stream and forces the sent data to be displayed.
Write content in new window
Take a look at the following script program:
VaR win = Window. Open ("", "win", "width = 300, Height = 200"); // a window object
Win.doc ument. Open ("text/html", "replace ");
Win.doc ument. Write ("<HTML> <Body> Hello, world! </Body> Win.doc ument. Close ();
The first statement opens a new window. It uses an empty document parameter ("") and the return value is allocated to the variable win. Then we use win.doc ument, the new document object, to write some HTML. Defining "replace" is very necessary, because we do not want a blank page to have one in the history.
Because we process the same document object, we may need to allocate another variable to it:
VaR win = Window. Open ("", "win", "width = 300, Height = 200"); // a window object
VaR Doc = win.doc ument;
Doc. Open ("text/html", "replace ");
Doc. Write ("<HTML> World! </Body> Doc. Close ();
We can also use the with statement:
VaR win = Window. Open ("", "win", "width = 300, Height = 200"); // a window object
With (win.doc ument ){
Open ("text/html", "replace ");
Write ("<HTML> World! </Body> Close ();
}

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.