简体   繁体   English

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

[英]JavaScript script dynamic script loading IE6 issue

I am using an in house custom library to replicate jsonp calls. 我正在使用内部自定义库来复制jsonp调用。 Before you guys ask me to use JQuery or other libraries, let me tell I cannot use it due to some constraints. 在你们要求我使用JQuery或其他库之前,让我告诉我由于某些限制我无法使用它。

Below is the code used to make the request: 以下是用于发出请求的代码:

 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); }; 

This works the first time it is executed. 这在第一次执行时有效。 On subsequent executions, the request is made but the callback is not executed. 在后续执行中,发出了请求,但未执行回调。

If I use a sleep method(commented in the code) as below, the callback gets executed on subsequent calls also. 如果我使用如下的sleep方法(在代码中有注释),则回调也会在后续调用中执行。

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

How does the sleep influence the execution of the call back? 睡眠如何影响回叫的执行? Works fine in Firefox though. 尽管在Firefox中工作正常。

First time through, this.script.src.readyState is false (script still loading) and a handler is created for the onLoad event. 第一次,this.script.src.readyState为false (脚本仍在加载),并为onLoad事件创建了一个处理程序。 When onLoad is triggered, the anonymous onLoad function is called - which appears to be very similar to the onReadyStateChange handler. 触发onLoad时,将调用匿名onLoad函数-似乎与onReadyStateChange处理程序非常相似。 However, onLoad is triggered only once, so your function is called only once. 但是,onLoad仅触发一次,因此您的函数仅调用一次。

The sleep slows execution enuf so your this.script.src.readyState is true when you execute the if statement. 睡眠会使执行enuf变慢,因此在执行if语句时,this.script.src.readyState为true

What do you think? 你怎么看?

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

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