繁体   English   中英

JavaScript脚本动态脚本加载IE6问题

[英]JavaScript script dynamic script loading IE6 issue

我正在使用内部自定义库来复制jsonp调用。 在你们要求我使用JQuery或其他库之前,让我告诉我由于某些限制我无法使用它。

以下是用于发出请求的代码:

 BurrpJsonAjaxRequest.prototype.send = function() { this.script = document.createElement("script"); this.script.type = "text/javascript"; this.script.charset = "UTF-8"; this.script.src = this.URL; if (typeof(this.callBack) == "function") { this.script.callback = this.callBack; } var currRequest = this; //sleep(100); if (this.script.src.readyState) { //IE this.script.src.onreadystatechange = function() { if (this.script.src.readyState == "loaded" || this.script.src.readyState == "complete") { this.script.src.onreadystatechange = null; currRequest.hideLoading(); currRequest.callback(); } }; } else { //Others this.script.src.onload = function() { currRequest.hideLoading(); currRequest.callback(); }; } this.docHead.appendChild(this.script); }; 

这在第一次执行时有效。 在后续执行中,发出了请求,但未执行回调。

如果我使用如下的sleep方法(在代码中有注释),则回调也会在后续调用中执行。

 function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i milliseconds){ break; } } } 

睡眠如何影响回叫的执行? 尽管在Firefox中工作正常。

第一次,this.script.src.readyState为false (脚本仍在加载),并为onLoad事件创建了一个处理程序。 触发onLoad时,将调用匿名onLoad函数-似乎与onReadyStateChange处理程序非常相似。 但是,onLoad仅触发一次,因此您的函数仅调用一次。

睡眠会使执行enuf变慢,因此在执行if语句时,this.script.src.readyState为true

你怎么看?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM