Ajax:将代码注入 Internet Explorer
我无法让以下代码在 Internet Explorer 中工作,它似乎不想执行通过 Ajax 从服务器发回的代码,它什么也没做:
var ajax = new ActiveXObject('Microsoft.XMLHTTP');
ajax.open('GET','http://fromsitewebsite.com/javascript.js',true);
ajax.setRequestHeader('Connection','close');
ajax.onreadystatechange = function()
{
if ( ajax.readyState == 4 )
{
document.body.innerHTML += '<script type="text/javascript">'+ajax.responseText+'</script>';
}
};
ajax.send('');
我已经尝试这样做,但仍然没有运气;
document.body.innerHTML += '<script type="text/javascript">('+ajax.responseText+')()</script>')
干杯
I'm having trouble getting the follow code to work in Internet Explorer, it doesn't seem to want to execute the code sent back from the server via Ajax, it just does nothing:
var ajax = new ActiveXObject('Microsoft.XMLHTTP');
ajax.open('GET','http://fromsitewebsite.com/javascript.js',true);
ajax.setRequestHeader('Connection','close');
ajax.onreadystatechange = function()
{
if ( ajax.readyState == 4 )
{
document.body.innerHTML += '<script type="text/javascript">'+ajax.responseText+'</script>';
}
};
ajax.send('');
I've tried doing this with still no luck;
document.body.innerHTML += '<script type="text/javascript">('+ajax.responseText+')()</script>')
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
让 IE 处理脚本内容正确标记,您需要设置.text值。
To get IE to handle the content of the script tag properly, you need to set the .text value.
你为什么不尝试一下:
如果我没记错的话,这会按预期工作
Why don't you try:
If I remember correctly, this works as expected
尝试
eval
返回的代码。Try to
eval
the returned code.您可能必须创建脚本节点,然后通过“innerText”属性而不是“innerHTML”设置其内容。无论如何,这是一件很奇怪的事情。您只需添加一个脚本元素并将其“src”属性设置为您在 AJAX 调用中使用的 URL。
当你说它“什么都不做”时,你检查过脚本错误吗?
You might have to create the script node and then set its content via the "innerText" attribute instead of "innerHTML". That's kind-of a weird thing to do anyway. You could just add a script element and set its "src" attribute to the URL you're using in the AJAX call.
When you say it "just does nothing", have you checked for script errors?
如果是 IE,则需要使用 execScript 方法
eval,这是上面推荐的,在 IE 中有一些特定的
In case of IE you need to use execScript method
eval, which is recommended above, has some specific in IE
有几件事。
首先,eval() 是邪恶的。如果您在重型 javascript 驱动的应用程序中使用它,它会显着减慢速度。
另外,为什么要加载 JavaScript 代码?一个好主意是考虑其他方法。如果它是一个小脚本,只需加载它,用户就会缓存它并且加载时间会很舒服。如果您想要将服务器值添加到页面,请使用 AJAX 并加载 JSON。如果 javascript 文件很大,请尝试缩小它并使用 gzip 从服务器传送它。如果不是上述情况,IE 支持脚本上的一个名为 defer="defer" 的属性,它将呈现并执行您新添加的代码。但我不推荐它,因为它仅受 IE 支持。
..弗雷德里克
A few things.
First of all eval() is evil. If you use it in a heavy javascript driven application it will slow down it significantly.
Also, why load javascript code? A good idea is to think of an other approach. If it's a small script just have it loaded, the user will cache it and load times will be comfortable. If it's server values you want added to your page use AJAX and load JSON instead. If the javascript file is large try minify it and deliver it from the server using gzip. If non of the above, IE supports a attribute on the script called defer="defer" it will render and execute your new added code. But I wouldn't recommend it since it's only supported by IE.
..fredrik