检测Firefox中的关闭窗口事件 [英] Detect Close window event in Firefox

查看:130
本文介绍了检测Firefox中的关闭窗口事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的目的是只触发消息框,但我不知道这是一个非常问的问题,但相信我,我没有通过网络找到答案。如果用户点击关闭(X)按钮。
如果用户点击后退/前进按钮,并且还使用F5,CTRL + R,...



我不想关联窗口关闭按钮点击后面的任何其他操作,将有一个会话与Ajax杀死。所以如果用户输入F5按钮,就不能接受会话。



这是我的代码。有关信息,我知道在IE浏览器中有一种方法来检查事件对象clientY,但这在Firefox中不起作用。
$ b $ pre $ $ $ $ $ > $(a)。click(function(){
window.onbeforeunload = null;
});

window.onbeforeunload = function(e){
var closewindowmessage =如果你离开这个页面,你的会话将被关闭。

e = e || window.event;
//对于IE和Firefox
if(e){
e.returnValue = closewindowmessage;
}

//对于Safari
返回closewindowmessage;
};


解决方案

没有确定的方式正在卸载。您可以重建您的网站,使用现在非常流行的锚定导航方法,它将数据存储在HTML锚点中,例如 http://www.example.com/page#something=something 。这至少可以解决后退/前进按钮的问题,而不是当用户重新加载页面时。



除此之外,您可以使用各种在用户尝试卸载页面之前跟踪鼠标和键盘操作的特设方法。例如,你可以跟踪用户拖动鼠标向右对角 - 这可能意味着他正要关闭窗口/选项卡,所以保留消息。对角线左边 - 这可能意味着他只是点击后退按钮或者输入地址字段。如果你真的认真,就要研究人们如何移动光标,并把它与他们是要关闭页面还是做一些允许的相关联。然后,再次,在Mac上,关闭按钮位于窗口上方的左侧角落。等等等等。它还可以是最好的猜测。您可以跟踪向上的鼠标移动,并在浏览器视口中显示一个红色的大消息(不是弹出/警报),以警告用户还没有考虑离开网页。

跟踪键盘事件有一点确定性,但仍需要一些跨浏览器和平台研究。我留给你这个代码,我希望可以作为一个样板。它记录按键并按下F5或Apple + R(Mac)时的消息。否则,它将显示一条消息,其中包含所有记录的按键列表。



分析需要测试和扩展;它只在Firefox Mac上测试过。我可以立即指出的一个错误是,如果按Apple + R,R,您仍然会得到提示,因为第二个页面实例从未记录Apple密钥的任何keydown事件 - 仅针对R密钥。如果用户按下中间的东西,如Apple + L,R,也会失败。你可能没有问题,只要检查最后一个按键是否是R.

 < script> 
//创建一个空数组。
window.keys = [];

//记录每个按键
window.onkeydown = function(e){
var evt = window.event || Ë;

var keyCode = e.keyCode || e.which;

window.keys.push(keyCode)



函数analyzeKeyPresses(){
keys.reverse(); //反转数组,以便处理。
var doBlock = true;

//如果数组中有足够的键,我们只应用一些检查。不想要JS错误...
switch(window.keys.length){
case 0:
doBlock = true; //冗余如果没有按键记录,假设我们应该提示用户。
break;
默认值://记录两个或更多按键。
if(keys [0] == 82&& keys [1] == 224)doBlock = false; //用户在Mac上按下了apple + r - 不要提示!
if(keys [0] == 82&& keys [1] == 17)doBlock = false; //用户在Windovs上按ctrl + r(未经测试) - 不要提示!

//注意:没有休息!故意降落!我们还想检查F5!
案例1://记录一个或多个按键。
if(keys [0] == 116)doBlock = false; //用户按F5 - 不要提示!
}

keys.reverse(); //如果我们需要再次使用它,请取消反向数组。 (更容易阅读...)
返回doBlock;


window.onbeforeunload = function(e){
var closewindowmessage = window.keys.join();

var blockUnload = analyzeKeyPresses();

if(blockUnload){
e = e || window.event;
//对于IE和Firefox
if(e){
e.returnValue = closewindowmessage;
}

//对于Safari
返回closewindowmessage;
}
};

< / script>
< a href =#1> 1< / a> < a href =#2> 2< / a>


I know it is a very asked question, but believe me I don't find the answer through the Web.

My purpose is to trigger the message box only if the user clicks on the close (X) button. The user continues to get the message box if he clicks on the back/forward button and also if he uses F5, CTRL+R, ...

I do not want to associate any other action than the window close button click as behind, there will be a session kill with Ajax. So it is not acceptable to kill the session if the user types F5 button.

Here is my code. For info, I know that there is a way in IE to check the event object clientY, but this does not work in Firefox.

$("a").click(function () {
window.onbeforeunload = null;
});

window.onbeforeunload = function (e) {
var closewindowmessage="If you leave this page, your session will be definitely closed.";

  e = e || window.event;
  // For IE and Firefox
  if (e) {
    e.returnValue = closewindowmessage;
  }

  // For Safari
  return closewindowmessage;
};

解决方案

There's no definitive way of detecting why/how a page is being unloaded. You could rebuild your site to use the now ever so popular "anchor navigation" method which stores data in the HTML anchor, such as http://www.example.com/page#something=something. This would at least typically solve the problem for the back/forward buttons but not when the user is reloading the page.

Other than that, you could employ various ad hoc ways of tracking the mouse and keyboard action before the user tries to unload the page. You could for example track when the user drags the mouse diagonally up to the right – that probably means he's just about to close the the window/tab, so keep the message. Diagonally up to the left – that probably means he's just about to click the back forward buttons or maybe enter something to the address field. If you're really serious, conduct a study of how people move the cursor and correlate that with whether they're about to close the page or do something "allowed". Then again, on a Mac the close button is in the upper left corner of the window. And so on and so forth. It'll still just be best guesses.

You could also track upward mouse movements and show a big red message in the browser viewport (not a popup/alert) to warn the user before he even considers leaving the page.

Tracking keyboard events is a little bit more deterministic, but still requires some cross browser and platform research. I leave you with this code, which I'm hoping may work as a boilerplate. It logs the key presses and suppresses the message if F5 or Apple+R (Mac) was pressed. Otherwise it will show a message containing a list of all logged key presses.

The analysis needs testing and extension; it's only been tested on Firefox Mac. One bug that I can immediately point out is that if you press Apple+R,R you'll still get prompted because the second page instance never recorded any keydown event for the Apple key – only for the R key. It will also fail if the user presses something inbetween, like Apple+L,R. You might be fine with just checking if the last key pressed was R.

<script>
// Create an empty array.
window.keys = [];

// Log every key press
window.onkeydown = function (e) {
  var evt = window.event || e;

  var keyCode = e.keyCode || e.which;

  window.keys.push(keyCode)

}

function analyzeKeyPresses(){
  keys.reverse();   // Reverse the array so it's easier to handle.
  var doBlock = true;

  // Here we only apply certain checks if there are enough keys in the array. Don't want a JS error...
  switch(window.keys.length){
    case 0:
      doBlock = true;  // Redundant. If there are no key presses logged, assume we should prompt the user.
      break;
    default: // Two or more key presses logged.
      if(keys[0] == 82 && keys[1] == 224) doBlock = false;  // User pressed apple+r on a Mac - don't prompt!
      if(keys[0] == 82 && keys[1] == 17)  doBlock = false;  // User pressed ctrl+r on Windovs (untested) - don't prompt!

      // Note: No break! Intentional fall-through! We still want to check for F5!
    case 1:  // One or more key presses logged.
      if(keys[0] == 116) doBlock = false;   // User pressed F5 - don't prompt!
  }

  keys.reverse();   // Un-reverse the array in case we need to use it again. (Easier to read...)
  return doBlock;
}

window.onbeforeunload = function (e) {
  var closewindowmessage=window.keys.join(" ");

  var blockUnload = analyzeKeyPresses();

  if(blockUnload){
    e = e || window.event;
    // For IE and Firefox
    if (e) {
      e.returnValue = closewindowmessage;
    }

    // For Safari
    return closewindowmessage;
  }
};

</script>
<a href="#1">1</a> <a href="#2">2</a> 

这篇关于检测Firefox中的关闭窗口事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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