JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

15.10.2 The document.write() Method

The document.write() method used in the code above is the subject of the next section.


15.10.2 The document.write() Method

The document.write() method was one of the very first scriptable APIs implemented by the Netscape 2 web browser. It was introduced well before the DOM and was the only way to display computed text in a document. It is no longer needed in new code, but you are likely to see it in existing code.

document.write()concatenates its string arguments and inserts the resulting string into the document at the location of the script element that invoked it. When the script finishes running, the browser parses the generated output and displays it. The following code, for example, uses write() to dynamically output information into an otherwise static HTML document:


It is important to understand that you can use the write() method to output HTML to the current document only while that document is being parsed. That is, you can call document.write() from within top-level code inelements only because these scripts are executed as part of the document parsing process. If you place a document.write() call within a function definition and then call that function from an event handler, it will not work as you expect—in fact, it will erase the current document and the scripts it contains! (You’ll see why shortly.) For similar reasons, you should not use document.write() in scripts that have the defer or async attributes set.

Example 13-3 in Chapter 13 used document.write() in this way to generate more complicated output.

You can also use the write() method to create entirely new documents in other windows or frames. (When working with multiple windows or frames, however, you must be careful not to violate the same-origin policy.) Your first call to the write() method of another document will erase all content in that document. You can call write()more than once to build up the new content of the document. The content you pass to write()may be buffered (and not displayed) until you terminate the sequence of writes by calling the close()method of the document object. This, in essence, tells the HTML parser that it has reached the end-of-file for the document and that it should finish parsing and display the new document.

It is worth noting that the Document object also supports a writeln() method, which is identical to the write() method in every way except that it appends a newline after outputting its arguments. This can be useful if you are outputting preformatted text within a

 element, for example.


The document.write() method is not commonly used in modern code: the innerHTML property and other DOM techniques provide a better way of adding content to a document. On the other hand, some algorithms do lend themselves nicely to a stream-style I/O API like that provided by the write()method. If you are writing code that computes and outputs text while it runs, you might be interested in Example 15-10 , which wraps simple write() and close() methods around the innerHTML property of a specified element.

Example 15-10. A streaming API for the innerHTML property

// Define a simple "streaming" API for setting the innerHTML of an element.

function ElementStream(elt) {

if (typeof elt === "string") elt = document.getElementById(elt);

this.elt = elt;

this.buffer = ""; }

// Concatenate all arguments and append to the buffer ElementStream.prototype.write = function() {

this.buffer += Array.prototype.join.call(arguments, ""); };

// Just like write(), but add a newline ElementStream.prototype.writeln = function() {

this.buffer += Array.prototype.join.call(arguments, "") + "\n"; };

// Set element content from buffer and empty the buffer.

ElementStream.prototype.close = function() {

this.elt.innerHTML = this.buffer;

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27902.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1