Skip to content

Instantly share code, notes, and snippets.

@hakobe
Last active November 20, 2017 00:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakobe/5243675 to your computer and use it in GitHub Desktop.
Save hakobe/5243675 to your computer and use it in GitHub Desktop.
外部 script の読み込みのタイミングによってはdocument.writeが無視される場合がある
<html>
<!--
"HTML 構文解析器が HTML 文書の末尾まで来たタイミングより後で <script src> で
指定されたスクリプト経由で実行される document.write は黙って無視される" by wakabatan
see also http://www.whatwg.org/specs/web-apps/current-work/#ignore-destructive-writes-counter
$ plackup -p 3005 -e 'use Plack::App::Directory; Plack::App::Directory->new({root=>"."})->to_app'
のようにしておためしください
-->
<body>
<div id="target"></div>
<script>
var scriptElem = document.createElement('script');
scriptElem.src = '/write.js';
var targetElem = document.getElementById('target');
targetElem.appendChild(scriptElem); // このscriptElemで読み込まれるコードは非同期で実行される
</script>
<h1>h1desu</h1>
</body>
</html>
console.log('hello1!'); // ログが出力される
document.write('<p>hello!!!</p>'); // documentに出力されない
console.log('hello2!'); // ログが出力される
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment