Loading JavaScript Into Div Dynamically
<p>I need to load JavaScript code (which I don't have control over) from a URL into a div. The JavaScript code is a bunch of document.write() statements. Once the document.write() statements finish executing, I need to extract the resulting text from the div using jQuery or JavaScript and use that text for something else. Currently, I am doing the following to load the JavaScript into the div: </p>

<pre><code>    $('body').append('&lt;div id="mydiv" style="display: none"&gt;&lt;/div&gt;');
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    $('#mydiv').append(script);
</code></pre>

<p>How would I know when the document.write statements have finished executing and I can safetly extract the text out of the div and use it? Or, is there a better way of doing this?</p>
