当用户尝试刷新/关闭/导航到另一个网页时显示确认对话框 [英] Displaying a confirm dialog box when the user tries to refresh/close/navigate to another web page

查看:74
本文介绍了当用户尝试刷新/关闭/导航到另一个网页时显示确认对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图刷新页面,导航到另一个页面,关闭页面时,我想用一个确认对话框警告用户。

I would like to warn the user with a confirm dialog box when he tries to refresh the page, navigate to another page, close the page.

就像Gmail一样做;例如,当你写一些东西然后尝试关闭页面而不发送你的消息时,会出现一个警告对话框,有两个选项:
离开此页/留在这个页面

Just like Gmail does; for example when you write something then try to close the page without sending your message, a warning dialog box will appear with two options: Leave this page/Stay on this page

推荐答案

使用onbeforeunload。来自 https://developer.mozilla.org/en/DOM/window.onbeforeunload

Use onbeforeunload. From https://developer.mozilla.org/en/DOM/window.onbeforeunload:

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

  // For IE and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Your text';
  }

  // For others
  return 'Your text';
};

这篇关于当用户尝试刷新/关闭/导航到另一个网页时显示确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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