El método abre un documento para escritura (writing)

Esto viene con algunos efectos secundarios. Por ejemplo:

  • Todos las atenciones de eventos actualmente registrados en el documento, los nodos dentro del documento o la ventana del documento son eliminados.
  • Todos los nodos existentes se eliminan del documento.





Valor devuelto

Una instancia del objeto Document (Document).


El código simple a continuación abre el documento y reemplaza su contenido con un número de diferentes fragmentos HTML antes de cerrarlo nuevamente.

document.write("<p>Hola mundo!</p>");
document.write("<p>Soy un pez</p>");
document.write("<p>El numero es 42</p>");


Nota: Traducción pendiente para el texto que sigue

An automatic call happens when document.write() is called after the page has loaded.

For years Firefox and Internet Explorer additionally erased all JavaScript variables, etc., in addition to removing all nodes. This is no longer the case.document non-spec'ed parameters to

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, 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. See Security check basics for more about principals.


There is a lesser-known and little-used three-argument version of , which is an alias of (see its page for full details).

This call, for example opens in a new window, with its opener set to null:

js"", "", "noopener=true");


Browsers used to support a two-argument, with the following signature:

js, replace);

Where type specified the MIME type of the data you are writing (e.g. text/html) and replace if set (i.e. a string of "replace") specified that the history entry for the new document would replace the current history entry of the document being written to.

This form is now obsolete; it won't throw an error, but instead just forwards to (i.e. is the equivalent of just running it with no arguments). The history-replacement behavior now always happens.


HTML Standard
# dom-document-open-dev

Compatibilidad del Navegador

BCD tables only load in the browser

See also