Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil). Junte-se a nós e ajude-nos a fazer o trabalho!
Você também pode ler o artigo em English (US).

Writes a string of text to a document stream opened by

Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls, which will clear the document.




A string containing the text to be written to the document.



  <title>write example</title>

    function newContent() {
      alert("load new content");;
      document.write("<h1>Out with the old - in with the new!</h1>");

<body onload="newContent();">
  <p>Some original document content.</p>



Writing to a document that has already loaded without calling will automatically call Once you have finished writing, it is recommended to call document.close() to tell the browser to finish loading the page. The text you write is parsed into the document's structure model. In the example above, the h1 element becomes a node in the document.

If the document.write() call is embedded within an inlined HTML <script> tag, then it will not call For example:

  document.write("<h1>Main title</h1>") 
Note: document.write and document.writeln do not work in XHTML documents (you'll get a "Operation is not supported" [NS_ERROR_DOM_NOT_SUPPORTED_ERR] error in the error console). This happens when opening a local file with the .xhtml file extension or for any document served with an application/xhtml+xml MIME type. More information is available in the W3C XHTML FAQ.
Note: document.write in deferred or asynchronous scripts will be ignored, and you'll get a message like "A call to document.write() from an asynchronously-loaded external script was ignored" in the error console.
Note: In Edge only, calling document.write more than once in an iframe causes the error SCRIPT70: Permission denied.
Note: Starting in 55, Chrome will not execute <script> elements injected via document.write() in case of an HTTP cache miss for users on a 2G connection.


Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes ? Yes ? ? ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes ? Yes ? ? ?

See also

Etiquetas do documento e colaboradores

Última atualização por: fscholz,