Javascript: Run "document.write" in createElement script failed
<p>I have a problem with <code>document.write</code>. Now, I have a page to load script dynamically. So, I use <code>document.createElement</code> to create a <code>&lt;script&gt;</code>. And in the javascript file, I use <code>document.write</code> to create a <code>dom</code> or <code>iframe</code> element. But it does not work in every browser. Why not?</p>

<p>Code in homepage:</p>

<pre><code>var script = document.createElement('script');
script.setAttribute('src', 'http://yoururl/test.js');
document.getElementsByTagName('head')[0].appendChild(script);
</code></pre>

<p>Code in script test.js:</p>

<pre><code>(function(){document.write('hello');})();
</code></pre>
