我知道在Internet Explorer(Pre-IE9)中你不能使用document.addEventListener()
,而是必须使用document.attachEvent
。我遇到的问题是document.attachEvent('onload', AddExternals);
根本没有做任何事情。在控制台中,输出应如下所示:
- document.attachEvent
- Adding Externals...
- jQuery loaded!
但是在IE8中,控制台输出是:
有没有明显的理由说明为什么会出现在下面的代码中?
if (document.addEventListener) {
console.log("document.addEventListener")
document.addEventListener("DOMContentLoaded", AddExternals);
} else if (document.attachEvent) {
console.log("document.attachEvent")
document.attachEvent("onload", AddExternals);
}
function AddExternals(){
console.log("Adding Externals...");
var jq = document.createElement("script");
jq.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(jq);
jq.onload = function(){console.log("jQuery loaded!")};
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js";
}
修改
我已更改document.attachEvent("onload", AddExternals)
,现在控制台正在输出document.attachEvent
和Adding Externals...
,但该功能永远不会完成?
答案 0 :(得分:1)
据我所知,没有strHDLocation = "C:\Test\file1.zip"
Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
xmlHttp.Open "GET", "http:downloadsite/report-id=123456", False, "myidhere", "mypwhere"
xmlHttp.Send()
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write xmlHttp.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
事件。相反,您可以使用document.onload
作为后备。您可能还必须测试文档状态以确保它尚未加载(例如,事件已经被触发)。
对于经过完全测试的函数,可以在任何级别的浏览器中知道文档何时就绪,请参阅此先前问题/答案中的代码:pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it
请记住,旧版本的IE没有window.onload
的脚本标记,因此您不一定会看到该控制台消息,但您的脚本仍应加载。有一个更复杂的方案,可以通知您何时加载旧版本的IE:javascript notify when script is loaded dynamically in IE
我建议您将脚本更改为:
.onload
相关变化:
function AddExternals(){
var doneLoad = false;
function onload() {
if (!doneLoad) {
doneLoad = true;
console.log("jQuery loaded!")
}
}
console.log("Adding Externals...");
var jq = document.createElement("script");
jq.type = "text/javascript";
jq.onload = doneLoad;
jq.onreadystatechange= function () {
if (script.readyState == "loaded" || script.readyState == "complete"){
doneLoad();
}
};
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js";
document.getElementsByTagName("head")[0].appendChild(jq);
}
。