Search code examples
javascriptdocument.write

Javascript:document.write("hello world") works at first but


Javascript:document.write("hello world") works at first but if you change the text it does not work for the next text.

To reproduce: In your address bar:

  1. Type http://nazmulweb.com or any other url

  2. After loading the page, type Javascript:document.write("hello world")

  3. Browser will show "hello world", isn't it?

  4. Then type javascript:document.write('hello stackoverflow users!')

This time the browser doesn't update the text. What can be the reason?


"What is my target?"


I am using the following code to write in an Iframe

function writeInIframe(data){   
var previewWindow=$("pwFrame").contentWindow;
if (previewWindow.document) {
    previewWindow.document.open();
    previewWindow.document.write(data); 
    previewWindow.document.close();
}
}

For the first time data is properly shown in the Iframe but later on, newly data is not updated in the Iframe.


Solution

  • From MDC:

    Writing to a document that has already loaded without calling document.open() will automatically perform a document.open 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.