Javascript:检查用户是否使用表单提交或单击链接进行导航 [英] Javascript: Check if the user is navigating away with a form submit or a simple link click

查看:111
本文介绍了Javascript:检查用户是否使用表单提交或单击链接进行导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在用户离开页面时提醒用户,这可以通过window.onUnload事件来处理。
但我还需要检查用户是否通过在页面上提交表单或单击导航链接进行导航。
我可以使用窗体的onSubmit事件来设置一个标志,然后在window.onUnload事件中检查该标志,但我不确定哪一个先触发。

I need to do remind the users something when they are leaving the page, and that can be handled with the window.onUnload event. But I also need to check if the user is navigating away by submitting the form on the page, or by clicking the navigation links. I could use the form's onSubmit event to set a flag, and then check against that flag in the window.onUnload event, but I am not sure which one fires first.

有什么想法?

any ideas ?

推荐答案

您实际上需要window.onbeforeunload

You actually want window.onbeforeunload

window.onbeforeunload = function (e) {
  var e = e || window.event;

  // For IE and Firefox
  if (e) {
    e.returnValue = 'Are You Sure?';
  }

  // For Safari
  return 'Are You Sure?';
};

这篇关于Javascript:检查用户是否使用表单提交或单击链接进行导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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