Ajax:将代码注入 Internet Explorer

发布于 2024-08-08 04:23:59 字数 641 浏览 2 评论 0原文

我无法让以下代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

对风讲故事 2024-08-15 04:23:59

让 IE 处理脚本内容正确标记,您需要设置.text值。

var scrElem = document.createElement('script');
scrElem.type = 'text/javascript';
scrElem.text = ajax.responseText;
document.body.appendChild(scrElem);

To get IE to handle the content of the script tag properly, you need to set the .text value.

var scrElem = document.createElement('script');
scrElem.type = 'text/javascript';
scrElem.text = ajax.responseText;
document.body.appendChild(scrElem);
长安忆 2024-08-15 04:23:59

你为什么不尝试一下:

var scriptElmnt  = document.createElement('SCRIPT');
scriptElmnt.type = 'text/javascript';
scriptElmnt.src  = '/javascript.js';
document.body.appendChild(scriptElmnt);

如果我没记错的话,这会按预期工作

Why don't you try:

var scriptElmnt  = document.createElement('SCRIPT');
scriptElmnt.type = 'text/javascript';
scriptElmnt.src  = '/javascript.js';
document.body.appendChild(scriptElmnt);

If I remember correctly, this works as expected

心作怪 2024-08-15 04:23:59

尝试eval返回的代码。

Try to eval the returned code.

开始看清了 2024-08-15 04:23:59

您可能必须创建脚本节点,然后通过“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?

留一抹残留的笑 2024-08-15 04:23:59

如果是 IE,则需要使用 execScript 方法

if ( ajax.readyState == 4 )  
{  
    if (window.execScript)  
        window.execScript(ajax.responseText);  
    else  
        document.body.innerHTML += '<script type="text/javascript">'+ajax.responseText+'</  script>';  
}

eval,这是上面推荐的,在 IE 中有一些特定的

In case of IE you need to use execScript method

if ( ajax.readyState == 4 )  
{  
    if (window.execScript)  
        window.execScript(ajax.responseText);  
    else  
        document.body.innerHTML += '<script type="text/javascript">'+ajax.responseText+'</  script>';  
}

eval, which is recommended above, has some specific in IE

绳情 2024-08-15 04:23:59

有几件事。

首先,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文