javascript beforeunload检测刷新与关闭 [英] javascript beforeunload detect refresh versus close
问题描述
使用以下功能可以检测用户按下刷新按钮或关闭按钮的哪个按钮?
?如果没有,还有另外一种方法吗?
$ $ $ $ $ $ $ $(window).bind('beforeunload',function(event){
返回'请保存我们的工作';
});
简单的答案是否定的 - 浏览器的安全模型不允许您明确检测用户选择离开您的页面的方式(刷新/关闭/内部链接/外部链接)。
$ b $ b 可以 - 使用cookie - 检查用户何时加载您的网页,而不管它们是否曾在同一会话中的该网站上 - 例如检测它们是否已刷新 - 但不在刷新之前: 并且不完美的方法是在页面卸载之前检测他们是否按下了F5或Ctrl + R或Cmd + R(用于刷新的键盘快捷键)。这将检测到一些刷新,但不是用户实际点击刷新按钮的地方。 您还可以检测何时点击链接是否链接是否在同一个网站上: Using the following function is it possible to detect which button the user has
pressed the refresh button or the close button? If not is there another way?
The simple answer is no - browsers' security models do not allow you to explicitly detect in what way a user has chosen to leave your page (refresh / close / internal link / external link). detect refresh browser for unload/beforeunload when browser closed It is possible - using a cookie - to check when a user loads your page whether they were previously on that site in the same session - e.g. detect if they have refreshed - but not before they refresh: Detect Browser Refresh in Javascript Check if page gets reloaded or refreshed in Javascript A partial and imperfect approach would be to detect whether they pressed "F5" or "Ctrl+R" or "Cmd+R" (keyboard shortcuts for refresh) just before the page unload. This will detect some refreshes, but not where the user actually clicked the refresh button. You can also detect when a link is clicked whether the target of the link is on the same site or not:
How can I detect when the user is leaving my site, not just going to a different page? 这篇关于javascript beforeunload检测刷新与关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
(function ($){
var refreshKeyPressed = false;
var modifierPressed = false;
var f5key = 116;
var rkey = 82;
var modkey = [17,224,91,93];
//检查刷新键
$(document).bind(
'keydown',
function evt){
//检查刷新
if(evt.which == f5key || window.modifierPressed&& evt.which == rkey){
refreshKeyPressed = true;
}
//检查修饰符
if(modkey.indexOf(evt.which)> = 0){
modifierPressed = true;
}
$ b $ //检查刷新键
$(document).bind(
'keyup',
function evt){
//检查撤销键
if(evt.which == f5key || evt.which == rkey){
refreshKeyPressed = false;
}
//检查修饰符
if(modkey.indexOf(evt.which)> = 0){
modifierPressed = false;
}
}
);
$ b $(window).bind('beforeunload',function(event){
var message =not refreshed;
if(refreshKeyPressed){
message =刷新;
}
event.returnValue =消息;
返回消息;
});
}(jQuery));
如何检测用户何时离开我的网站,而不只是去不同的页面? $(window).bind('beforeunload', function(event) {
return 'pls save ur work';
});
(function($) {
var refreshKeyPressed = false;
var modifierPressed = false;
var f5key = 116;
var rkey = 82;
var modkey = [17, 224, 91, 93];
// Check for refresh keys
$(document).bind(
'keydown',
function(evt) {
// Check for refresh
if (evt.which == f5key || window.modifierPressed && evt.which == rkey) {
refreshKeyPressed = true;
}
// Check for modifier
if (modkey.indexOf(evt.which) >= 0) {
modifierPressed = true;
}
}
);
// Check for refresh keys
$(document).bind(
'keyup',
function(evt) {
// Check undo keys
if (evt.which == f5key || evt.which == rkey) {
refreshKeyPressed = false;
}
// Check for modifier
if (modkey.indexOf(evt.which) >= 0) {
modifierPressed = false;
}
}
);
$(window).bind('beforeunload', function(event) {
var message = "not refreshed";
if (refreshKeyPressed) {
message = "refreshed";
}
event.returnValue = message;
return message;
});
}(jQuery));