SCRIPT tag removed from AJAX response in Internet Explorer

吃可爱长大的小学妹 提交于 2019-12-11 03:42:08

问题


In our current project some code is returned with AJAX and we use innerHTML to place this code inside a DIV.

Now we search this DIV for all available script tags and EVAL() the contents of these script tags (adds some information to a global array, etc)

scriptTags = responseElement.getElementsByTagName("script");

for (i = 0; i < scriptTags.length; i++) {
            eval(scriptTags[i].text);
        }

This works perfect in Firefox and Chrome. But in IE scriptTags appears to be empty. Upon further investigation it seems that the innerHTML of the responseElement doesn't contain any SCRIPT tags (while in Firefox / Chrome they're there!)

Anyone know the cause and/or work around?

By the way, this is how I put the AJAX response on the page:

this.proxy.innerHTML = o.responseText;
scriptTags = this.proxy.getElementsByTagName("script");

Debugging o.responseText shows the SCRIPT tags inside the response. Navigating to this.proxy element on the page shows no SCRIPT tag in IE but does show up in Firefox / Chrome.


回答1:


The problem was fixed by adding an additional node element before the SCRIPT tags that were in de AJAX response. (solution/cause: http://allofetechnical.wordpress.com/2010/05/21/ies-innerhtml-method-with-script-and-style-tags/)

In the future we should change our application so that the HTML is sent back as JSON.



来源:https://stackoverflow.com/questions/8430141/script-tag-removed-from-ajax-response-in-internet-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!