xmlhttp.responseText 可以包含 <script type="text/javascript src="..."></script>和负载? [英] Can xmlhttp.responseText contain <script type="text/javascript src="..."></script> and load?
问题描述
脚本执行 window.open('',...) 然后通过执行 innerHTML=xmlhttp.responseText 写入 xmlhttp.responseText,但脚本没有加载.
通常,您会以纯 Javascript 形式获得 xmlhttp 请求响应,然后使用 Javascript eval() 函数:
function callback() {var 结果 = xmlHttp.responseText;评估(结果);}
在这种情况下,您不会将其视为 HTML.相反,您将返回纯代码.区别:
不要在你调用的页面中这样做
改为这样做
alert('xmlhttprequest 已加载!');
所以,实际上就是这样:
function callback() {var 结果 = xmlHttp.responseText;eval("alert('xmlhttprequest 已加载!');");}
但您不希望这种情况发生:
function callback() {var 结果 = xmlHttp.responseText;eval("<script>alert('xmlhttprequest loaded!');</script>");}
这样做有一些问题,例如 eval 可能很慢.Google javascript eval 看看别人怎么说.
=== 编辑 ===
使用 DOM 方法而不是 xmlhttprequest 实际上可能是原始发布者在这里需要做的,即动态加载 Google 验证码.
<头></头><身体><script type="text/javascript">var head = document.getElementsByTagName('head')[0];var script = document.createElement('script');script.type = '文本/javascript';script.src = "http://google.com/captcha/location";head.appendChild(脚本);</html>
此外,如果您需要它降落在某个地方,您可以执行以下操作:
<头></头><身体><div id="验证码">
<script type="text/javascript">var captcha = document.getElementById('captcha');var script = document.createElement('script');script.type = '文本/javascript';script.src = "http://google.com/captcha/location";验证码.appendChild(脚本);</html>
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>
这篇关于xmlhttp.responseText 可以包含 <script type="text/javascript src="..."></script>和负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!