3

I am using the document.write( HTML ) to change a document html and it re-renders the page by itself, which is exactly what I want, but it does not show the same behavior on IE, as following:

 document.open();
 document.write(<HTML>);
 document.close();

So I tried the following :

document.documentElement.innerHTML  = <HTML>;

But it did not render the page unless I refresh it.

So what is the difference between them, and what is the best way to change the document content and re-render it?

Hasan Al-Natour
  • 1,456
  • 1
  • 12
  • 22
  • 2
    @andrusieczko I think this is just an example placeholder ... – Martin Tournoij Jan 14 '15 at 10:17
  • 1
    A good answer would probably need to be rather long and detailed. Maybe you should describe what you want, instead of posing a theoretical question. Exactly how do you want to change a document? Presumably adding something specified with an HTML fragment (a real example would be more useful than a dummy notation like ``), but *where*? And why would you start *creating* a new document with the `open()` method if you actually want to *change* a document? – Jukka K. Korpela Jan 14 '15 at 15:32
  • 1
    Two obvious differences: `.write()` requires doctype declaration and `` tags whereas `innerHTML` wants head and body only; and when only changing the document's `innerHTML` then all the scripts on the page continue running. – Bergi Jan 14 '15 at 15:56
  • How does `document.documentElement.innerHTML = ` make any sense? That produces `` – Waxi Jan 15 '15 at 14:03

1 Answers1

0

Your first approach is not a good practice - Why is document.write considered a "bad practice"?

second approach should work. can you put that on jsbin or jsfiddle? anyway, it is not a good approach (unless you got the html as a string from ajax or similar)

The best approach is to do DOM manipulation.

Changes in DOM should be immediate. You should not need to refresh it.

Community
  • 1
  • 1
Luís Soares
  • 4,466
  • 1
  • 27
  • 47