可当用户停止加载文档的JavaScript检测?可当、加载、文档、用户

2023-09-10 14:13:48 作者:干掉上帝我就是神

我实施彗星使用脚本标记长轮询技术的基础上,this页。

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.

第二个问题,虽然是如果用户实际点击停止,然后在浏览器停止加载我的脚本标记,我必须依靠超时重启彗星。这意味着,如果我超时是,说20秒,页面可能无法在用户更新后的20秒单击停止。我的问题是:有没有什么办法来检测他们这样做的时候?我能察觉,当他们使用preSS逃逸的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 (使用延迟的属性<脚本> 标记) - <脚本> 资源将块进一步处理的文件。如果<脚本> 资源请求无法完成,页面的的onload 事件永远不会触发(也有其他副作用还有,如果有以后的内容<脚本> 在DOM: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".

从账面上,你也可以通过添加一个初始以提高性能&LT;脚本&GT; 在当DOM被加载时,有一个延迟属性的IE浏览器和切割为其他浏览器的连接,当你想到的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 为了让这个答案是最新的......在延迟属性的提供在现在大部分现代浏览器。的

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