Наши волонтёры ещё не перевели данную статью на Русский. Присоединяйтесь к нам и помогите сделать эту работу!
Вы можете также прочитать эту статью на English (US).

The Document.open() method opens a document for writing.




// In this example, the document contents are 
// overwritten as the document 
// is reinitialized on open(). 
document.write("<html><p>remove me</p></html>"); 
// document is empty.


If a document exists in the target, this method clears it (see the example above).

Also, an automatic document.open() call happens when document.write() is called after the page has loaded, but that's not defined in the W3C specification. document non-spec'ed parameters to document.open

Do not confuse this method with window.open(). document.open allows you to overwrite the current document or append to it, while window.open provides a way to open a new window, leaving the current document intact. Since window is the global object, just calling open(...) does the same as window.open(...). You can close the opened document using document.close().

See Security check basics for more about principals.

If you dont want to create a history entry, replace open() with open("text/html", "replace").


Specification Status Comment
HTML Living Standard
The definition of 'document.open()' in that specification.
Living Standard  
Document Object Model (DOM) Level 2 HTML Specification
The definition of 'document.open()' in that specification.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?


Full support  
Full support
Compatibility unknown  
Compatibility unknown

Gecko-specific notes

Starting with Gecko 1.9, this method is subject to the same same-origin policy as other properties, and does not work if doing so would change the document's origin.

Starting with Gecko 1.9.2, document.open() uses the principal of the document whose URI it uses, instead of fetching the principal off the stack. As a result, you can no longer call document.write() into an untrusted document from chrome, even using wrappedJSObject.

Метки документа и участники

Обновлялась последний раз: mdnwebdocs-bot,