0

when I add the script tag in body tag of my index.html directly, like below:

<script type="text/javascript" src='**doc_write_in_it.js**'></script>

It works well, the "test doc write" is output there.

But if I write in another way, like below:

<script type="text/javascript">
    var model = document.createElement('script');
    model.setAttribute('type','text/javascript');
    model.setAttribute('src','doc_write_in_it.js');
    var bd = document.getElementsByTagName('body')[0];
    bd.appendChild(model);
</script>   

document.write become invalid within Javascript file which is added by appendChild.

The alert in doc_write_in_it.js will show, but the text in document.write doesn't.

doc_write_in_it.js file is like this:

alert('activited');
document.write('test doc write");

Hope someone can help... Thanks a lot...

auCrux
  • 1
  • 1
  • Check here http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – elclanrs Aug 16 '13 at 06:18
  • did you try adding onload around your code or having is last before the body closes? – Sergio Aug 16 '13 at 06:18
  • I'd tried to put it in a function, but the result is the same, 'alert' show itself or 'console.log' show itself, except 'document.write' – auCrux Aug 16 '13 at 08:06

3 Answers3

1

as mentioned above, document.write does not work when the page has already been loaded. I suggest use innerHTML property whenever you can.

example:

var bd = document.getElementsByTagName('body')[0];
bd.innerHTML = "some text";
Novellizator
  • 9,169
  • 9
  • 38
  • 58
0

document.write will overwrite anything else before it. So, if you are absolutely sure that it is what you want to be doing. Then you should probably wait for the page to load, and then fire the document.write. Something on the lines of the below..

<script>
function my_onload_fn() {
    document.write("test document write");
}
</script>

<body onload="my_onload_fn();">
v2b
  • 1,328
  • 7
  • 14
  • In my case, the 'document.write' seems deactivated or for some other reasons, it doesn't output anything, even if I put it in window.onload function. – auCrux Aug 16 '13 at 06:35
  • @auCrux so what do you see? does the alert show up? and document.write doesn't.. or none of them execute? – v2b Aug 16 '13 at 06:41
  • @auCrux in the doc_write_in_it.js, put the alert and document.write in a function.. and call that function onload on the html page. i am wondering if the document.write is overwriting the page even before anything else can happen... – v2b Aug 16 '13 at 06:45
  • I'd tried to put it in a function, but the result is the same, 'alert' show itself or 'console.log' show itself, except 'document.write' – auCrux Aug 16 '13 at 08:05
0

Actually, document.write can't affects when page is loaded, but you can invoke document.open() to make it posible.

Putting script code in script tag is synchronous, while loading script file via DOM manipulation is asynchronous, so,if the page is simple, page may have been loaded when document.write(..) runs and make document.write(...) do nothing if document.open() is not invoked.

alert('activited');
document.open();
document.write('test doc write');
document.close();

This will work. And check this link to learn more.

Fly_pig
  • 155
  • 8