动态添加脚本标签加载的HTML - 错误的IE7和8脚本、加载、错误、标签

2023-09-10 21:19:05 作者:秀不羁

我一直停留在这个相当长的一段 - 任何帮助将是非常美联社preciated

I've been stuck on this for quite a while - any help would be really appreciated.

基本上我加载HTML到一个页面,但加载的HTML需要包含在其内的特定位置外部脚本。我发现做到这一点的最好方法是使用下面的code:

Basically I'm loading HTML into a page, but the loaded HTML needs to contain an external script in specific position within it. I found the best way to do this was using the following code:

$('#blah').load('blah.htm',
    function(){
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = "[external script url + query string here]";
        $('#blah_blah')[0].appendChild(script);                 
});

编辑: 我使用的appendChild,而不是原因jQuery的追加是$ .append增加了脚本完全错误的元素创建的录像机。如果我使用的appendChild,它被添加到指定的正确的元素。我想这可能跟jQuery的插入方法,这个答案中概述: jQuery:无法追加<脚本>元素 的

The reasons I'm using "appendChild" instead of jQuery's "append" is that $.append adds the videoplayer created by the script to completely the wrong element. If I use appendChild, it is added to the correct element specified. I think this maybe to do with jQuery's insertion methods, as outlined within this answer: jQuery: Can't append <script> element

在code我使用的伟大工程的Firefox和Chrome,但在IE7和放大器; 8我得到错误信息无法获得属性的值的appendChild:对象为null或undefined,和脚本标签不能在DOM中可以看出

The code I'm using works great in Firefox and Chrome, but in IE7 & 8 I get the error message "Unable to get value of the property 'appendChild': object is null or undefined", and the script tag cannot be seen in the DOM.

如果我有行:

$("#blah_blah")[0].appendChild(document.createTextNode("test"));

文本节点测试被添加到DOM和可以看出在页面上 - 。因此它似乎的appendChild不工作,但有一个问题,追加脚本标记

the text node "test" is added to the DOM, and can be seen on the page - so it seems "appendChild" does work, but there is an issue with appending a script tag.

难道有可能是一个IE浏览器特定的安全问题?

Could it possibly be an IE specific security issue?

推荐答案

有什么问题:

   $.getScript("script.js");