The script does window.open('',...) and then writes xmlhttp.responseText by doing innerHTML=xmlhttp.responseText, but the script doesn't load.
Typically, you would get the xmlhttp request response as pure Javascript and then use the Javascript eval() function:
function callback() {
var result = xmlHttp.responseText;
eval(result);
}
In this case, you would NOT treat it as HTML. Instead, you would return pure code. The difference:
Don't do this in your page you call
<script type="text/javascript">
alert('xmlhttprequest loaded!');
</script>
Do this instead
alert('xmlhttprequest loaded!');
So that, in effect this is what happens:
function callback() {
var result = xmlHttp.responseText;
eval("alert('xmlhttprequest loaded!');");
}
But you don't want this to happen:
function callback() {
var result = xmlHttp.responseText;
eval("<script>alert('xmlhttprequest loaded!');</script>");
}
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.
=== EDIT ===
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.
<html>
<head></head>
<body>
<script type="text/javascript">
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);
</script>
</body>
</html>
Additionally, if you need it to land somewhere, you can do something like:
<html>
<head></head>
<body>
<div id="captcha">
</div>
<script type="text/javascript">
var captcha = document.getElementById('captcha');
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "http://google.com/captcha/location";
captcha.appendChild(script);
</script>
</body>
</html>
Take a look at this write-up
eval(xmlhttp.responseText);
You might want to eval the responseText in the javascript.
You might want to make sure that the responseText comes from your own server-side system, to avoid XSS attacks.
© 2022 - 2024 — McMap. All rights reserved.