Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Typically, you would get the xmlhttp request response as pure Javascript and then use the Javascript eval() function:</p> <pre><code>function callback() { var result = xmlHttp.responseText; eval(result); } </code></pre> <p>In this case, you would NOT treat it as HTML. Instead, you would return pure code. The difference:</p> <p><strong>Don't do this in your page you call</strong></p> <pre><code>&lt;script type="text/javascript"&gt; alert('xmlhttprequest loaded!'); &lt;/script&gt; </code></pre> <p><strong>Do this instead</strong></p> <pre><code>alert('xmlhttprequest loaded!'); </code></pre> <p>So that, in effect this is what happens:</p> <pre><code>function callback() { var result = xmlHttp.responseText; eval("alert('xmlhttprequest loaded!');"); } </code></pre> <p>But you don't want this to happen:</p> <pre><code>function callback() { var result = xmlHttp.responseText; eval("&lt;script&gt;alert('xmlhttprequest loaded!');&lt;/script&gt;"); } </code></pre> <p>There are some issues associated with doing it this way, such as eval can be slow. Google javascript eval to see what others have to say.</p> <p>=== EDIT ===</p> <p>Using a DOM method as opposed to xmlhttprequest may actually be what the original poster is needing to do here, which is load a Google captcha code dynamically.</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.type = 'text/javascript'; script.src = "http://google.com/captcha/location"; head.appendChild(script); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Additionally, if you need it to land somewhere, you can do something like:</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;div id="captcha"&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; var captcha = document.getElementById('captcha'); var script = document.createElement('script'); script.type = 'text/javascript'; script.src = "http://google.com/captcha/location"; captcha.appendChild(script); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload