Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To append content to a document, you should only call <code>document.write()</code> during the &lt;script>-block parsing phase.</p> <p>If you have a call to <code>document.write</code> somewhere where it executes outside that context, such as in your event handler, there is no parsing going on, so nowhere for that string <code>'foo'</code> to go.</p> <p>What should happen according to the <a href="http://docs.sun.com/source/816-6408-10/document.htm#1221642" rel="noreferrer">original JavaScript reference</a> is:</p> <blockquote> <p>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.</p> </blockquote> <p>That is, it assumes you've made a mistake and meant to do:</p> <pre><code>document.open('text/html'); document.write(html); </code></pre> <p>Which replaces the entire document with new content. You'll also notice that because you didn't finish that off by calling:</p> <pre><code>document.close(); </code></pre> <p>...in Firefox, the ‘loading’ throbber is still animating, expecting more content to be written to the document (but it never comes).</p> <p>In this case, you are <code>document.write()</code>ing a string:</p> <pre><code>Hello bobince! How are you today? </code></pre> <p>that is clearly not a valid HTML document. Chrome (and Safari, which behaves the same) is trying to parse it as an HTML document, but fails because it is non-whitespace text content outside the document element (&lt;html>), which is not allowed. If you put some tags in the name input:</p> <pre><code>Hello &lt;em&gt;bobince&lt;/em&gt;! How are you today? </code></pre> <p>You'll see everything outside the &lt;em> element is discarded. Firefox and IE manage to apply their “fix up text outside any element” broken-HTML-rule to save you, but Chrome doesn't.</p> <p>If you want to <code>document.write()</code> a new document, you should make sure it is indeed a complete document:</p> <pre><code>&lt;html&gt;&lt;head&gt;&lt;title&gt;Greetings&lt;/title&gt;&lt;body&gt;&lt;p&gt;Hello bobince! How are you today?&lt;/p&gt;&lt;/body&gt;&lt;/html&gt; </code></pre> <p>Or, if you want to <code>document.write()</code> a text string, you should theoretically call:</p> <pre><code>document.open('text/plain'); </code></pre> <p>before the write(). But beware, the mimetype parameter is not supported by Chrome or Safari; they'll always treat what you write as HTML.</p> <p>In any case, <strong>you probably don't want to be using this method</strong>. If you want to add to (or change) the existing page you should normally be using DOM methods, such as setting the text on a status output div, or replacing the body contents using innerHTML.</p> <p><code>document.write()</code> is needed in a few places (in particular, when you are creating a new window or frame from scratch), but otherwise it's generally the wrong thing and should be viewed with suspicion.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload