检测导航取消JavaScript或jQuery的 [英] Detecting a cancel in navigation with Javascript or jQuery

查看:113
本文介绍了检测导航取消JavaScript或jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果有一种方法来检测用户$ P $使用JavaScript pssing在浏览器中的停止导航按钮(或者甚至更好,jQuery的。)例如,如果你点击一个链接网页这需要一段时间来加载,你可能想显示旋转装载机。但是,如果用户取消什么的导航页面?反正是有检测摆脱你把纺纱装载机的?

I was curious if there was a way to detect the user pressing the "stop navigation" button in the browser using javascript (or, even better, jQuery.) For example, if you click a link for a webpage that takes a while to load, you may want to show a spinning loader. But what if the user cancels navigation to the page? Is there anyway to detect that to get rid of the spinning loader that you put?

修改:我做了一点研究,似乎有在JavaScript,但你不知道它的一个事件的onStop,的它只能在Internet Explorer 。如果任何人有任何其他想法来实现类似的onStop一个跨浏览器的解决方案,那简直是美妙的,但如果没有,我会回答我的问题在几天内关闭这一点。

EDIT: I did a bit more research, and there seems to be an onStop event in javascript but, wouldn't you know it, it only works in internet explorer. If anyone has any other ideas to implement a cross browser solution like onStop, that'd be wonderful, but if not, I'll answer my own question in a few days to close this.

编辑2 http://stackoverflow.com/a/16216193 说,这是不可能的。由于做了一些其他的答案。

EDIT 2: http://stackoverflow.com/a/16216193 says it's not possible. As do a few other answers.

推荐答案

好了的话,如许,我会回答我的问题。

Alright so, as promised, I'm going to answer my own question.

我已经想过这个颇有几分 - 我想出了一个解决方案。我无法使其在code的工作(我没有尝试太硬),但它在理论上应工作。

I've thought about this quite a bit - and I've come up with a solution. I wasn't able to make it work in code (I didn't try too hard), but it should work in theory.

所以,我想过当一个网页应该决定停止被称为决定的标准。我想出了这一点:

So I thought about the criteria of deciding when a webpage should decide stop was called. I came up with this:


  • 如果剧本没有的一段合理的时间后死亡,可以假定导航已取消。

然后一个jQuery的事件可以在或类似的东西被解雇。但什么是一段合理的金额是多少?我想这将是部分基于网页渲染时间(获取图像等)来获取用户的上网速度是一个想法。这可以通过做可以得到:

Then a jQuery event can be fired on the body or something like that. But what constitutes "a resonable amount of time?" I figured it would be partially based on page render time (fetching images, etc.) to get an idea of how fast the user's internet is. That can be gotten by doing:

var start = new Date();
var time;
$("body").load(function () {
    time = new Date() - start;
    ...
});

乘以系数(也许3或东西),并得到一个approxamate传输时间。 (这将不得不进行调整以考虑它需要多长时间让服务器生成下一个页面,依赖于它是如何动态的)。然后,利用这个新发现的时间* 3 你会这样写:

$("a").click(function() { //Anything that could go to another page should filter through here
    setInterval(function() {$(document).trigger("navstopped");},time*3);
}
$(document).on("navstopped") {
    //Do stuff now that we assume navigation stopped.
}

假设。这是真正的所有我们在这里所做。我们可能有不一致的互联网连接,快一分钟慢下。服务器负载可能不一致了。也许这是煮好的图像,例如此页忍者,但它击中了一堆请求了下,使其产生/服务翻页慢一点。所以,我们只是假设的东西打断导航一些如何,的但我们并不一定

现在,当然,这可以在IE的的onStop 事件一起使用,但是这确实是唯一一个跨浏览器的解决方案,我能想到的。我无法得到它的工作,但也许一些jQuery神或许能够在未来。

Now, of course, this could be used in conjunction with IE's onStop event, but this was really the only cross browser solution I could think of. I wasn't able to get it to work, but maybe some jQuery god may be able to in the future.

修改,再发表:我张贴此之前,即使我有另外的想法。更多的浏览器支持 onAbort 。如果我们有一个永远载入图像,用户presses停止,将 onAbort 被解雇?即使另一个网页加载?它要求测试,但可能工作了。我喜欢我的第一个想法虽然好。虽然不稳定,但比这繁琐的想法更加稳定,我意识到这可能是不好的做法。

Edit before post: Even before I posted this, I had another idea. More browsers support onAbort. If we have a picture that never loads, and the user presses stop, will onAbort be fired? Even if another webpage is loading? It requires testing but that may work too. I like my first idea better though. Although unstable, it is more stable than this cockamamie idea and I realize this could be bad practice.

这篇关于检测导航取消JavaScript或jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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