您的位置:首页 > Web前端 > JavaScript

javascript的Document对象——方法

2006-10-28 10:49 375 查看
方法
captureEvents
Sets the document to capture all events of the specified type.
方法源 document
实现版本 Navigator 4.0

语法
captureEvents(eventType)
参数
eventType The type of event to be captured. The available event types are listed with the event object.

描述
When a window with frames wants to capture events in pages loaded from different locations (servers), you need to use Window.captureEvents in a signed script and precede it with Window.enableExternalCapture. For more information and an example, see Window.enableExternalCapture.
captureEvents works in tandem with releaseEvents, routeEvent, and handleEvent. For more information, see "Events in Navigator 4.0".

close
Closes an output stream and forces data sent to layout to display.
方法源 document
实现版本 Navigator 2.0

语法
close()
参数
无。

描述
The close method closes a stream opened with the document.open method. If the stream was opened to layout, the close method forces the content of the stream to display. Font style tags, such as BIG and CENTER, automatically flush a layout stream.
The close method also stops the "meteor shower" in the Netscape icon and displays Document: Done in the status bar.

示例
The following function calls document.close to close a stream that was opened with document.open. The document.close method forces the content of the stream to display in the window.
function windowWriter1() {
var myString = "Hello, world!"
msgWindow.document.open()
msgWindow.document.write(myString + "<P>")
msgWindow.document.close()
}

参看
document.open, document.write, document.writeln

getSelection
Returns a string containing the text of the current selection.
方法源 document
实现版本 Navigator 4.0

语法
getSelection()
描述
This method works only on the current document.

安全性
You cannot determine selected areas in another window.

示例
If you have a form with the following code and you click on the button, JavaScript displays an alert box containing the currently selected text from the window containing the button:
<INPUT TYPE="BUTTON" NAME="getstring"
VALUE="Show highlighted text (if any)"
onClick="alert('You have selected:/n'+document.getSelection());">

handleEvent
调用指定事件的控制句柄。
方法源 document
实现版本 Navigator 4.0

语法
handleEvent(event)
参数
event 你想要调用的对象的某一事件控制句柄的名称。

描述
要获得关于事件句柄的更多信息,请看“关于事件的常规信息”。

open
Opens a stream to collect the output of write or writeln methods.
方法源 document
实现版本 Navigator 2.0
Navigator 3.0: 添加了 "replace" parameter; document.open() or document.open("text/html") clears the current document if it has finished loading

语法
open(mimeType, replace)
参数
mimeType (Optional) A string specifying the type of document to which you are writing. If you do not specify mimeType, text/html is the default.
replace (Optional) The string "replace". If you supply this parameter, mimeType must be "text/html". Causes the new document to reuse the history entry that the previous document used.

描述
Sample values for mimeType are:

text/html specifies a document containing ASCII text with HTML formatting.

text/plain specifies a document containing plain ASCII text with end-of-line characters to delimit displayed lines.

image/gif specifies a document with encoded bytes constituting a GIF header and pixel data.

image/jpeg specifies a document with encoded bytes constituting a JPEG header and pixel data.

image/x-bitmap specifies a document with encoded bytes constituting a bitmap header and pixel data.

plugIn loads the specified plug-in and uses it as the destination for write and writeln methods. For example, "x-world/vrml" loads the VR Scout VRML plug-in from Chaco Communications, and "application/x-director" loads the Macromedia Shockwave plug-in. Plug-in MIME types are only valid if the user has installed the required plug-in software.
The open method opens a stream to collect the output of write or writeln methods. If the mimeType is text or image, the stream is opened to layout; otherwise, the stream is opened to a plug-in. If a document exists in the target window, the open method clears it.
End the stream by using the document.close method. The close method causes text or images that were sent to layout to display. After using document.close, call document.open again when you want to begin another output stream.

In Navigator 3.0 and later, document.open or document.open("text/html") clears the current document if it has finished loading. This is because this type of open call writes a default <BASE HREF=> tag so you can generate relative URLs based on the generating script's document base.

The "replace" keyword causes the new document to reuse the history entry that the previous document used. When you specify "replace" while opening a document, the target window's history length is not incremented even after you write and close.

"replace" is typically used on a window that has a blank document or an "about:blank" URL. After "replace" is specified, the write method typically generates HTML for the window, replacing the history entry for the blank URL. Take care when using generated HTML on a window with a blank URL. If you do not specify "replace", the generated HTML has its own history entry, and the user can press the Back button and back up until the frame is empty.

After document.open("text/html","replace") executes, history.current for the target window is the URL of document that executed document.open.

示例
示例 1. The following function calls document.open to open a stream before issuing a write method:
function windowWriter1() {
var myString = "Hello, world!"
msgWindow.document.open()
msgWindow.document.write("<P>" + myString)
msgWindow.document.close()
} 示例 2. The following function calls document.open with the "replace" keyword to open a stream before issuing write methods. The HTML code in the write methods is written to msgWindow, replacing the current history entry. The history length of msgWindow is not incremented.

function windowWriter2() {
var myString = "Hello, world!"
msgWindow.document.open("text/html","replace")
msgWindow.document.write("<P>" + myString)
msgWindow.document.write("<P>history.length is " +
msgWindow.history.length)
msgWindow.document.close()
} The following code creates the msgWindow window and calls the function:

msgWindow=window.open('','',
'toolbar=yes,scrollbars=yes,width=400,height=300')
windowWriter2() 示例 3. In the following example, the probePlugIn function determines whether a user has the Shockwave plug-in installed:

function probePlugIn(mimeType) {
var havePlugIn = false
var tiny = window.open("", "teensy", "width=1,height=1")
if (tiny != null) {
if (tiny.document.open(mimeType) != null)
havePlugIn = true
tiny.close()
}
return havePlugIn
} var haveShockwavePlugIn = probePlugIn("application/x-director")

参看
document.close, document.write, document.writeln, Location.reload, Location.replace

releaseEvents
Sets the document to release captured events of the specified type, sending the event to objects further along the event hierarchy.
方法源 document
实现版本 Navigator 4.0

Note
If the original target of the event is a window, the window receives the event even if it is set to release that type of event.

语法
releaseEvents(eventType)
参数
eventType Type of event to be captured.

描述
releaseEvents works in tandem with captureEvents, routeEvent, and handleEvent. For more information, see "Events in Navigator 4.0".

routeEvent
Passes a captured event along the normal event hierarchy.
方法源 document
实现版本 Navigator 4.0

语法
routeEvent(event)
参数
event Name of the event to be routed.

描述
If a subobject (document or layer) is also capturing the event, the event is sent to that object. Otherwise, it is sent to its original target.
routeEvent works in tandem with captureEvents, releaseEvents, and handleEvent. For more information, see "Events in Navigator 4.0".

write
Writes one or more HTML expressions to a document in the specified window.
方法源 document
实现版本 Navigator 2.0
Navigator 3.0: users can print and save generated HTML using the commands on the File menu

语法
document.write(expr1, ...,exprN)
参数
expr1, ... exprN Any JavaScript expressions.

描述
The write method displays any number of expressions in the document window. You can specify any JavaScript expression with the write method, including numeric, string, or logical expressions.
The write method is the same as the writeln method, except the write method does not append a newline character to the end of the output.

Use the write method within any SCRIPT tag or within an event handler. Event handlers execute after the original document closes, so the write method implicitly opens a new document of mimeType text/html if you do not explicitly issue a document.open method in the event handler.

You can use the write method to generate HTML and JavaScript code. However, the HTML parser reads the generated code as it is being written, so you might have to escape some characters. For example, the following write method generates a comment and writes it to window2:

window2=window.open('','window2')
beginComment="/<!--"
endComment="--/>"
window2.document.write(beginComment)
window2.document.write(" This some text inside a comment. ")
window2.document.write(endComment)

Printing, saving, and viewing generated HTML
In Navigator 3.0 and later, users can print and save generated HTML using the commands on the File menu.
If you choose Document Source or Frame Source from the View menu, the web browser displays the content of the HTML file with the generated HTML. (This is what would be displayed using a wysiwyg: URL.)

If you instead want to view the HTML source showing the scripts which generate HTML (with the document.write and document.writeln methods), do not use the Document Source or Frame Source menu item. In this situation, use the view-source: protocol.

For example, assume the file file://c|/test.html contains this text:

<HTML>
<BODY>
Hello,
<SCRIPT>document.write(" there.")</SCRIPT>
</BODY>
</HTML> If you load this URL into the web browser, it displays the following:

Hello, there. If you choose View Document Source, the browser displays:

<HTML>
<BODY>
Hello,
there.
</BODY>
</HTML> If you load view-source:file://c|/test.html, the browser displays:

<HTML>
<BODY>
Hello,
<SCRIPT>document.write(" there.")</SCRIPT>
</BODY>
</HTML> For information on specifying the view-source: protocol in the location object, see the Location object.

示例
In the following example, the write method takes several arguments, including strings, a numeric, and a variable:
var mystery = "world"
// Displays Hello world testing 123
msgWindow.document.write("Hello ", mystery, " testing ", 123) In the following example, the write method takes two arguments. The first argument is an assignment expression, and the second argument is a string literal.

//Displays Hello world...
msgWindow.document.write(mystr = "Hello ", "world...") In the following example, the write method takes a single argument that is a conditional expression. If the value of the variable age is less than 18, the method displays "Minor." If the value of age is greater than or equal to 18, the method displays "Adult."

msgWindow.document.write(status = (age >= 18) ? "Adult" : "Minor")

参看
document.close, document.open, document.writeln

writeln
Writes one or more HTML expressions to a document in the specified window and follows them with a newline character.
方法源 document
实现版本 Navigator 2.0
Navigator 3.0: users can print and save generated HTML using the commands on the File menu

语法
writeln(expr1, ... exprN)
参数
expr1, ... exprN Any JavaScript expressions.

描述
The writeln method displays any number of expressions in a document window. You can specify any JavaScript expression, including numeric, string, or logical expressions.
The writeln method is the same as the write method, except the writeln method appends a newline character to the end of the output. HTML ignores the newline character, except within certain tags such as the PRE tag.

Use the writeln method within any SCRIPT tag or within an event handler. Event handlers execute after the original document closes, so the writeln method will implicitly open a new document of mimeType text/html if you do not explicitly issue a document.open method in the event handler.

示例
All the示例 used for the write method are also valid with the writeln method.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: