JavaScript可以在用户停止加载文档时检测吗? [英] Can JavaScript detect when the user stops loading the document?

查看:125
本文介绍了JavaScript可以在用户停止加载文档时检测吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用脚本标签长轮询技术实现Comet,基于这个页面

I'm implementing Comet using the script tag long polling technique, based on this page.

一个问题(我不认为有解决方案)是浏览器继续将文档显示为加载,并保留启用工具栏上的停止按钮。这样做是有道理的,因为文件正在加载,虽然不太理想,但我认为我可以生活在这里。

One issue (that I don't think there's a solution for) is the "throbber of doom" - the browser continues to show the document as "loading" forever and leaves the Stop button on the toolbar enabled. This kind of makes sense, because the document is still loading and while it's not ideal, I think I can live with it.

第二个问题是,如果用户实际上点击了Stop,那么浏览器停止加载我的脚本标签,我必须依靠一个超时来重新启动Comet。这意味着如果我的超时时间(例如20秒),用户点击停止后,页面可能不会更新最多20秒。我的问题是:有什么办法可以检测他们这样做吗?我可以使用 onkeydown 事件来检测他们什么时候使用转义,但如果他们使用工具栏按钮或菜单项停止加载,则不能。

The second issue, though, is that if the user actually clicks Stop then the browser stops loading my script tag and I have to rely on a timeout to restart Comet. This means that if my timeout is, say 20 seconds, the page may not be updated for up to 20 seconds after the user clicks Stop. My question is: is there any way to detect when they do this? I can detect when they press escape using the onkeydown event, but not if they use the toolbar button or menu item to stop loading.

解决方案需要在Firefox 3.5和Chrome 3.0中工作。

The solution needs to work in Firefox 3.5 and Chrome 3.0.

推荐答案

正如我在评论中所建议的那样...添加脚本 onload 事件。

As I suggested in the comments... add the script in the onload event.

编辑:我想我可以解释我的推理建议,希望它会帮助别人尝试保存相同的问题。

I guess I can explain my reasoning for the suggestion, hopefully it'll help others trying to save the same problem.

浏览器将继续加载(并因此播放慢速),直到触发 onload 事件在每个窗口(每个DOM树与全局窗口父)在给定的窗口或选项卡。许多外部资源可以并行下载,但默认情况下,除了IE 1 (使用 defer c $ c> c c $ c> c $ c>处理文件。如果< script> 资源请求从未完成,则页面的 onload 事件永远不会触发(还有其他副作用以及如果在DOM中有< script> 之后有内容,那么DOM可能永远不会被完全加载,之后的资源脚本> 可能根本无法加载),因此永远不会完成加载。

A browser will continue to "load" (and thus play the throbber) until it has fired the onload event for every "window" (each DOM tree with a global window parent) in a given window or tab. Many outside resources are able to download in parallel, but by default—and in fact, there is no escaping this default in any browser except IE1 (using the defer attribute on the <script> tag)—<script> resources will "block" further processing of the document. If the <script> resource request never completes, the page's onload event never fires (and there are other side effects as well, if there is content after the <script> in the DOM: the DOM may never be fully loaded, and resources after that <script> may never load at all), and thus never finishes "loading".

从那里开始,您也可以通过在DOM加载时添加初始< script> 来提高性能,IE的属性为 defer 并且当您希望 onload 事件将触发时,切割其他浏览器的连接(这很难准确地确定,可能需要实验)。

Carrying on from that, you may also be able to improve performance by adding an initial <script> at when the DOM is loaded, with a defer attribute for IE, and cutting the connection for other browsers when you expect the onload event would fire (this is hard to pinpoint exactly and may require experimentation).

1 为了使此答案保持最新... defer 属性是可用现在大多数现代浏览器。

1 To keep this answer up to date... the defer attribute is available in most modern browsers now.

这篇关于JavaScript可以在用户停止加载文档时检测吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆