动态 JS 加载中的错误处理

发布于 2024-08-10 13:09:14 字数 751 浏览 5 评论 0原文

仅当用户导航到页面上的该选项卡时,我才加载网页的一部分所需的 JS 文件。

var scriptFile = document.createElement("script");
scriptFile.setAttribute("type", "text/javascript");
scriptFile.setAttribute("src", scriptURL);

我正在尝试为这种动态加载提出一种错误处理机制,以便在 JS 加载超时或 JS 在 URL 指定的位置不可用时显示错误消息。

这就是我尝试实现错误处理(对于 IE)的方式:

scriptFile.onreadystatechange = function() { // For IE
    if (this.readyState == 'complete' || this.readyState == 'loaded') {
        // Display content in tab
    } else {
        // Display error message
    }
}

这导致了一个问题。当一切正常并且 JS 按预期出现时,我首先收到错误消息,然后收到正确的内容。

经过一番头脑风暴,我发现readyState先从“未初始化”变为“正在加载”,然后又变为“已加载”。当状态处于“正在加载”时,会显示错误消息。之后,状态更改为“已加载”,此时正在显示内容。

有什么想法如何处理这个问题,以便最初不显示错误?

I am loading a JS file needed only for one part of my web page, only when the user navigates to that tab on the page.

var scriptFile = document.createElement("script");
scriptFile.setAttribute("type", "text/javascript");
scriptFile.setAttribute("src", scriptURL);

I am trying to come up with an error handling mechanism for this dynamic loading, so that I can show an error message if the JS load times out or if the JS is unavailable in the location specified by the URL.

This is how I am trying to implement the error handling (for IE):

scriptFile.onreadystatechange = function() { // For IE
    if (this.readyState == 'complete' || this.readyState == 'loaded') {
        // Display content in tab
    } else {
        // Display error message
    }
}

This is causing one problem. When all is fine, and the JS is present as expected, I am getting the error message first, and then the correct content.

After some brainstorming, I found out that the readyState changes from "uninitialized" to "loading" first, and then to "loaded". Its when the state is at "loading" that the error message is being displayed. After that, the state is changing to "loaded" at which point the content is being displayed.

Any ideas how to handle this so the error is not displayed initially?

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

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

发布评论

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

评论(4

寒冷纷飞旳雪 2024-08-17 13:09:14

既然没有“哎呀我死了!”状态,则无法显示错误消息。

编辑:删除了代码,因为我的观点是你不能这样做。通过 AJAX 请求,您可以检查响应并查看它是否是 404,但不能使用脚本标记来执行此操作。

edit2:再想一想,为什么不通过 AJAX 获取脚本并注入 ''在 document.body 内?

要检查 AJAX 请求是否已被轰炸,请检查 XMLHTTPRequest 对象的 status 属性。如果是 404,那么,它就是 404。

此外,您不应该手动执行此操作。我总是建议使用 Prototype 或 jQuery。由于我已经被 Prototype 宠坏了,所以我不应该尝试解释如何检查请求的状态。为什么不在此处的另一个问题中询问如何处理 AJAX 请求?专业人士肯定会告诉您如何处理各种故障,而不仅仅是 404。

Since there is no "oops I died!" state, you can't display error messages.

edit: removed the code since my point is that you can't do that. With an AJAX request you can check the response and see if it's a 404 but you can't do that with a script tag.

edit2: on second thought, why don't you fetch the script via AJAX and inject a '<script type="text/javascript">' + responseText + '</script>' inside document.body?

To check if an AJAX request has bombed, check the XMLHTTPRequest object's status property. If it's 404, well, it's a 404.

Also, you shouldn't do this stuff by hand. I always suggest using Prototype or jQuery. Since I have been spoiled by Prototype, I shouldn't try to explain how to check for the request's status. Why don't you ask how to handle AJAX requests in another question here? The pros will certainly tell you how to handle all kinds of failures, not just 404.

眼趣 2024-08-17 13:09:14

怎么样让一个(全局)变量来检查它是否已加载(例如,脚本将在加载完成后设置它),然后使用计时器设置另一个函数(在加载函数之前已经在页面上)来检查该变量(例如 5秒延迟?)

如果未设置变量(在预定义的时间之后),则向用户显示错误。

what about having one (global) variable to check if it's loaded (e.g. the script will set it after loading finished) and then have another function (already on the page before the loading function) set with timer to check for that variable (e.g. 5 seconds delay?)

if the variable is not set (after the predefined amount of time), display error to the user.

千柳 2024-08-17 13:09:14

我认为你需要废弃“else”,因为它总是会经历“loaded”状态,无论它是否会工作,所以“else”分支没有做任何有用的事情。

相反,设置一个超时。

var scriptTimer = setTimeout(function() {
  // show error message
  // (should ideally precede with a check the content hasn't
  // been displayed, in case of race condition)
});
scriptFile.onreadystatechange = function() { // For IE
    if (this.readyState == 'complete' || this.readyState == 'loaded') {
       clearTimeout(scriptTimer);
       // now display content in tab
    }
}

I think you need to scrap the "else", as it will always go through that "loaded" state, whether it's going to work or not, so the "else" branch isn't doing anything useful.

Instead, set a timeout.

var scriptTimer = setTimeout(function() {
  // show error message
  // (should ideally precede with a check the content hasn't
  // been displayed, in case of race condition)
});
scriptFile.onreadystatechange = function() { // For IE
    if (this.readyState == 'complete' || this.readyState == 'loaded') {
       clearTimeout(scriptTimer);
       // now display content in tab
    }
}
别低头,皇冠会掉 2024-08-17 13:09:14

编辑:我的方法是错误的 - 按照 Kaze 的建议使用 AJAX

EDIT: my method was wrong - use AJAX as Kaze suggested

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