Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COI think this is not solving the problem. I am to have an <a> tag that opens a new windows, prompts the google-recaptcha query and then shows a text if captcha correct. I choose to get the google-recaptcha code, that contains javascript, by xmlhttprequest and the insert it by innerHTML. The is is there, but doesn't load and the recaptcha doesn't appear.
      singulars
    2. COYou're up against the security model of the browser if you do it that way. You have to load the javascript either through a native page load (using a script tag that embeds the code within the page or references a .js file with the code located there), or you need to eval() the code. The browser is blocking you loading the script tag to prevent malicious or unwanted code loading after page load. If you can post the content of the xmlhttprequest that you are trying to work with, that would help find a solution to your problem.
      singulars
    3. COWell, technically you can also use DOM methods to document.createElement('script') and then load through the src attribute. This still doesn't resolve your issue, however. One other method *may* be to use the old document.write('..html..including..<script>..tag..'), but there is a reason why ad servers go to so much trouble to obscure the document.write('<sc'+'ri'+'pt></sc'+'ri'+'pt>') tag within the html they load dynamically. These techniques are not nearly as useful as separating the html from the javascript and loading the javascript using eval or a separate page call on callback.
      singulars
 

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