document.write 存在几个问题?应该注意

  • document.write (henceforth DW) does not work in XHTML  

  • XHTML 不支持
  • DW executed after the page has finished loading will overwrite the page, or write a new page, or not work

  • 如果在页面加载完后执行,会覆盖整个页面 或者 输出一个新的页面 或 不能工作
  • DW executes where encountered: it cannot inject at a given node point

  • 不能在指定节点注入
  • DW is effectively writing serialised text which is not the way the DOM works conceptually, and is an easy way to create bugs (.innerHTML has the same problem)

 

 

 document.write VS appendChild

 someScript.js

    console.log('2');

 index1.htm

<script>
console.log('1');
 var script = document.createElement('script');
 script.src = 'someScript.js';
 document.write(script.outerHTML);
</script>
<script>
console.log('3');
</script>

index2.htm

<script>
console.log('1');
 var script = document.createElement('script');
 script.src = 'someScript.js';
 document.body.appendChild(script);
</script>
<script>
console.log('3');
</script>



结果: index1.html 1、2、3 index2.html 1、3、2 ;
posted @ 2016-05-26 14:40  czhyuwj  阅读(418)  评论(0编辑  收藏  举报