如何警告用户不要离开页面? [英] How to warn user not to leave the page?

查看:74
本文介绍了如何警告用户不要离开页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
未保存的数据"的Client/JS框架保护?

就像stackoverflow在询问页面上所做的那样,当我单击关闭按钮或网站上的任何其他链接时,会弹出一个对话框,询问您是否确定要离开该页面.如何在javascript或jQuery中做到这一点,您可以自定义框中的文本吗?

解决方案

您想听onbeforeunload事件:

window.onbeforeunload = function (e) {
  return 'Are you sure?';
};

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

Possible Duplicates:
How can I ask a web user for confirmation if he really wants to leave the page?
Client/JS Framework for “Unsaved Data” Protection?

Like stackoverflow does on the ask page, when I click the close button or any other link on the site a dialog pops up asking if you're sure you want to navigate away from the page. How to do that in javascript or jQuery and can you customize the text in the box?

解决方案

You want to listen to the onbeforeunload event:

window.onbeforeunload = function (e) {
  return 'Are you sure?';
};

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

这篇关于如何警告用户不要离开页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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