简体   繁体   English

在JQuery中处理F5

[英]Handling F5 in JQuery

I have a site where I would like to override F5 so that it doesn't refresh the page, but instead executes some ajax calls to refresh certain pieces. 我有一个网站,我想覆盖F5,以便它不刷新页面,而是执行一些ajax调用来刷新某些部分。 Is this possible? 这可能吗?

EDIT: Because none of you seem to understand why I would want to do something like this, if you are genuinely interested then visit these links: 编辑:因为你们似乎都不明白我为什么要做这样的事情,如果你真的有兴趣那么请访问这些链接:

Open-source project (simple web-terminal): http://code.google.com/p/web-terminal 开源项目(简单的网络终端): http//code.google.com/p/web-terminal

Running demo of simple web-terminal: http://web-terminal.net.pine.arvixe.com 简单的网络终端的运行演示: http//web-terminal.net.pine.arvixe.com

Live implementation (The forum version): http://www.u413.com 实时实施(论坛版): http//www.u413.com

Well, you could do that (at least in some browsers, I'm not sure if this works cross-browser), but it reaaalllly would be a pretty bad user experience. 好吧,你可以这样做(至少在某些浏览器中,我不确定这是否适用于跨浏览器),但它确实是一个非常糟糕的用户体验。

$(document).bind('keypress keydown keyup', function(e) {
    if(e.which === 116) {
       console.log('blocked');
       return false;
    }
    if(e.which === 82 && e.ctrlKey) {
       console.log('blocked');
       return false;
    }
});

Anyway, even if that works, there are other ways for an user to refresh the site. 无论如何,即使有效,用户还可以通过其他方式刷新网站。 Pressing ctrl + r ( cmd + r ) or just hit the refresh button. ctrl + rcmd + r )或只需按下刷新按钮。 Well, the other hot-key combination can get blocked similar, but no way to block the refresh button. 好吧,其他热键组合可以被阻止类似,但无法阻止刷新按钮。

-- -

It's maybe a huge better idea not to block those default browser behaviors, but to ask gracefully. 最好不要阻止那些默认的浏览器行为,而是优雅地提问。 That can be done by binding an event handler to the onbeforeunload event, which fires before a site is unloaded. 这可以通过将事件处理程序绑定到onbeforeunload事件来完成,该事件在卸载站点之前触发。

$(window).bind('beforeunload', function() {
    return 'Are you really sure?';
});

This is the same as the accepted answer above, except without capturing the 'keypress' event. 除了没有捕获'keypress'事件之外,这与上面接受的答案相同。

If you capture the 'keypress' event, you also block the 't' key. 如果您捕获'keypress'事件,则还会阻止't'键。 For some reason both the keycode and ASCII keycode are both captured if you use the 'keypress' event (which you can't see in the chrome debugger). 出于某种原因,如果使用'keypress'事件(在chrome调试器中无法看到),则会捕获键码和ASCII键码。 The F5 key is '116', but the ASCII keycode for 't' is also 116, so with the 'keypress' event you block F5, but you also block 't' app-wide. F5键为'116',但't'的ASCII键码也是116,所以使用'keypress'事件可以阻止F5,但你也可以阻止't'应用程序范围。

$(document).bind('keydown keyup', function(e) {
    if(e.which === 116) {
       console.log('blocked');
       return false;
    }
    if(e.which === 82 && e.ctrlKey) {
       console.log('blocked');
       return false;
    }
});

Here's the coffeescript just for fun :) 这是coffeescript只是为了好玩:)

$(document).bind "keydown keyup", (e) ->
    if e.keyCode is 116
      console.log "blocked"
      return false
    if e.keyCode is 82 and e.ctrlKey
      console.log "blocked"
      false

Here's one example on how overriding the F5 key will enhance user experience. 以下是关于如何覆盖F5键以增强用户体验的一个示例。 I'm building the newsletter editor for my client and I need to prevent the page from reloading when user accidentally presses F5 key but instead it refreshes email preview created from entered html code. 我正在为我的客户端构建新闻稿编辑器,当用户意外按下F5键时我需要阻止页面重新加载,而是刷新从输入的html代码创建的电子邮件预览。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM