检测浏览器中的离线模式的最佳方法是什么?

发布于 2024-07-04 14:26:11 字数 203 浏览 9 评论 0 原文

我有一个 Web 应用程序,其中有许多 Ajax 组件,它们经常在页面内刷新自己(它是某种仪表板)。

现在,我想向页面添加功能,以便在没有 Internet 连接时,页面的当前内容不会更改,并且页面上会出现一条消息,表明该页面已离线(目前,由于这些不同的小工具页面尝试刷新自己并发现没有连接,他们的旧数据消失了)。

那么,解决这个问题的最佳方法是什么?

I have a web application where there are number of Ajax components which refresh themselves every so often inside a page (it's a dashboard of sorts).

Now, I want to add functionality to the page so that when there is no Internet connectivity, the current content of the page doesn't change and a message appears on the page saying that the page is offline (currently, as these various gadgets on the page try to refresh themselves and find that there is no connectivity, their old data vanishes).

So, what is the best way to go about this?

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

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

发布评论

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

评论(9

冰雪梦之恋 2024-07-11 14:26:12

使用相关的 HTML5 API:在线/离线状态/事件

Use the relevant HTML5 API: online/offline status/events.

转角预定愛 2024-07-11 14:26:12

一种可能的解决方案是,如果页面和缓存页面具有不同的 url,则只需查看您所在的 url。 如果您位于缓存页面的 URL 上,则您处于离线模式。 博客很好地解释了 navigator.online 崩溃的原因

One possible solution is that if the page and the cached page have a different url to just look and see what url you are on. If you are on the url of the cached page then you are in offline mode. This blog makes a good point about why navigator.online is broke

奢欲 2024-07-11 14:26:11

向可靠的目的地发起呼叫,或者可能是一系列呼叫,如果用户具有活动的网络连接,则应该进行并返回这些呼叫 - 即使是像对 google、yahoo 和 msn 或类似的令牌 ping 这样简单的事情那。 如果至少有一个返回绿色,则表明您已连接。

Make a call to a reliable destination, or perhaps a series of calls, ones that should go through and return if the user has an active net connection - even something as simple as a token ping to google, yahoo, and msn, or something like that. If at least one comes back green, you know you're connected.

┼── 2024-07-11 14:26:11

我认为谷歌齿轮有这样的功能,也许你可以检查他们是如何做到的。

I think google gears have such functionality, maybe you could check how they did that.

戒ㄋ 2024-07-11 14:26:11

嗯,实际上,现在我稍微研究一下,比这更复杂一点。 请阅读 John Resig 的博客Mozilla 网站。 上面的海报也可能有一个很好的观点——无论如何你都会提出请求,所以你应该能够在它们失败时解决问题。这可能是一个更可靠的方法。

Hmm actually, now I look into it a bit, it's a bit more complicated than that. Have a read of these links on John Resig's blog and the Mozilla site. The above poster may also have a good point - you're making requests anyway, so you should be able to work out when they fail.. That might be a much more reliable way to go.

任性一次 2024-07-11 14:26:11
navigator.onLine

那应该可以满足你的要求。

您可能想在更新页面的任何代码中检查这一点。 例如:

if (navigator.onLine) {
    updatePage();
} else {
    displayOfflineWarning();
}
navigator.onLine

That should do what you're asking.

You probably want to check that in whatever code you have that updates the page. Eg:

if (navigator.onLine) {
    updatePage();
} else {
    displayOfflineWarning();
}
三岁铭 2024-07-11 14:26:11

请参阅HTML 5 草案规范。 您需要navigator.onLine。 并非所有浏览器都支持它。 Firefox 3 和 Opera 9.5 可以。

听起来好像你试图掩盖问题而不是解决问题。 如果失败的请求导致您的小部件清除其数据,那么您应该修复您的代码,以便它不会尝试更新您的小部件,除非收到响应,而不是尝试提前确定请求是否会成功。

See the HTML 5 draft specification. You want navigator.onLine. Not all browsers support it yet. Firefox 3 and Opera 9.5 do.

It sounds as though you are trying to cover up the problem rather than solve it. If a failed request causes your widgets to clear their data, then you should fix your code so that it doesn't attempt to update your widgets unless it receives a response, rather than attempting to figure out whether the request will succeed ahead of time.

野侃 2024-07-11 14:26:11

处理此问题的一种方法可能是使用显式超时方法扩展 XmlHTTPRequest 对象,然后使用该方法确定您是否在脱机模式下工作(即,对于不支持 navigator.onLine 的浏览器)。 以下是我在一个站点(使用 Prototype 库的站点)上实现 Ajax 超时的方法。 10 秒(10,000 毫秒)后,它中止调用并调用 onFailure 方法。

/**
 * Monitor AJAX requests for timeouts
 * Based on the script here: http://codejanitor.com/wp/2006/03/23/ajax-timeouts-with-prototype/
 *
 * Usage: If an AJAX call takes more than the designated amount of time to return, we call the onFailure
 *        method (if it exists), passing an error code to the function.
 *
 */

var xhr = {
    errorCode: 'timeout',
    callInProgress: function (xmlhttp) {
        switch (xmlhttp.readyState) {
            case 1: case 2: case 3:
                return true;
            // Case 4 and 0
            default:
                return false;
        }
    }
};

// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({
    onCreate: function (request) {
        request.timeoutId = window.setTimeout(function () {
            // If we have hit the timeout and the AJAX request is active, abort it and let the user know
            if (xhr.callInProgress(request.transport)) {
                var parameters = request.options.parameters;
                request.transport.abort();
                // Run the onFailure method if we set one up when creating the AJAX object
                if (request.options.onFailure) {
                    request.options.onFailure(request.transport, xhr.errorCode, parameters);
                }
            }
        },
        // 10 seconds
        10000);
    },
    onComplete: function (request) {
        // Clear the timeout, the request completed ok
        window.clearTimeout(request.timeoutId);
    }
});

One way to handle this might be to extend the XmlHTTPRequest object with an explicit timeout method, then use that to determine if you're working in offline mode (that is, for browsers that don't support navigator.onLine). Here's how I implemented Ajax timeouts on one site (a site that uses the Prototype library). After 10 seconds (10,000 milliseconds), it aborts the call and calls the onFailure method.

/**
 * Monitor AJAX requests for timeouts
 * Based on the script here: http://codejanitor.com/wp/2006/03/23/ajax-timeouts-with-prototype/
 *
 * Usage: If an AJAX call takes more than the designated amount of time to return, we call the onFailure
 *        method (if it exists), passing an error code to the function.
 *
 */

var xhr = {
    errorCode: 'timeout',
    callInProgress: function (xmlhttp) {
        switch (xmlhttp.readyState) {
            case 1: case 2: case 3:
                return true;
            // Case 4 and 0
            default:
                return false;
        }
    }
};

// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({
    onCreate: function (request) {
        request.timeoutId = window.setTimeout(function () {
            // If we have hit the timeout and the AJAX request is active, abort it and let the user know
            if (xhr.callInProgress(request.transport)) {
                var parameters = request.options.parameters;
                request.transport.abort();
                // Run the onFailure method if we set one up when creating the AJAX object
                if (request.options.onFailure) {
                    request.options.onFailure(request.transport, xhr.errorCode, parameters);
                }
            }
        },
        // 10 seconds
        10000);
    },
    onComplete: function (request) {
        // Clear the timeout, the request completed ok
        window.clearTimeout(request.timeoutId);
    }
});
以歌曲疗慰 2024-07-11 14:26:11

看来你已经回答了你自己的问题。 如果小工具发送异步请求并且超时,请不要更新它们。 如果有足够多的人这样做,则显示“页面离线”消息。

It seems like you've answered your own question. If the gadgets send an asynch request and it times out, don't update them. If enough of them do so, display the "page is offline" message.

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