HTML - DOM Document open() 方法



HTML DOM document **open()** 方法開啟一個文件以供寫入。文件的所有現有內容都將被清除。

語法

document.open();

引數

此方法接受下面列出的兩個引數,但這兩個引數都是可選的。

引數 描述
type 它指定文件的型別。其預設值設定為“text/html”。
replace 它指定新文件的歷史記錄條目將替換文件的當前歷史記錄條目。

返回值

它返回文件物件的例項。

HTML DOM Document 'open()' 方法的示例

以下示例說明了 open() 方法以開啟文件以供寫入。

開啟文件

在以下示例中,open() 方法用於開啟、寫入和關閉文件。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document open() Method
    </title>
</head>
<body>
    <button onclick="fun()">Click me</button>
    <script>
        function fun() {
            document.open();
            document.write("Welcome to Tutorials Point...");
            document.close();
        }
    </script>
</body>
</html>

在新視窗中開啟文件

在以下示例中,open() 方法用於在新視窗中開啟、寫入和關閉文件。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document open() Method
    </title>
</head>
<body>
    <p>Click to open a document in new window.</p>
    <button onclick="fun()">Click me</button>
    <script>
        function fun() {
            let x = window.open()
            x.document.open();
            x.document.write("Welcome to Tutorials Point...");
            x.document.close();
        }
    </script>
</body>
</html>

支援的瀏覽器

方法 Chrome Edge Firefox Safari Opera
open() 是 64 是 12 是 1 是 11 是 51
html_dom_document_reference.htm
廣告