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

发布于 2024-08-21 11:45:33 字数 1338 浏览 9 评论 0原文

我正在使用内部自定义库来复制 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 中运行良好。

I am using an in house custom library to replicate jsonp calls. Before you guys ask me to use JQuery or other libraries, let me tell I cannot use it due to some constraints.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-08-28 11:45:33

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

睡眠会减慢执行速度,因此当您执行 if 语句时, this.script.src.readyState 为 true

你怎么认为?

First time through, this.script.src.readyState is false (script still loading) and a handler is created for the onLoad event. When onLoad is triggered, the anonymous onLoad function is called - which appears to be very similar to the onReadyStateChange handler. However, onLoad is triggered only once, so your function is called only once.

The sleep slows execution enuf so your this.script.src.readyState is true when you execute the if statement.

What do you think?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文