单击浏览器的刷新按钮时如何弹出警告框? [英] How to pop up an alert box when the browser's refresh button is clicked?

查看:181
本文介绍了单击浏览器的刷新按钮时如何弹出警告框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户刷新有问题的页面,它会将另一条记录添加到数据库中,所以我想通过警告框警告用户,如果他们真的想刷新页面,如果他们点击确定那么页面应该刷新否则,如果他们点击取消则不会。

If the user refreshes the page in question it will add another record to the database, so I want to warn the user through an alert box if they really want to refresh the page and if they click ok then the page should be refreshed otherwise if they click cancel it won't be.

当浏览器的刷新按钮以跨浏览器兼容的方式点击时,如何显示此类型的警告框?

How to make this type of alert box appear when the browser's refresh button is clicked in a way that is cross browser compatible?

推荐答案

你可以这样做:

window.onbeforeunload = function() {
  return "Data will be lost if you leave the page, are you sure?";
};

这会向用户显示提示,允许他们取消。这不是特定的刷新,但是出于你的目的(比如在SO上编辑一个问题)这似乎并不重要,无论你要去哪里都会丢失信息。

This would show a prompt to the user allowing them to cancel. It's not refresh specific, but for your purposes (like editing a question on SO) that doesn't seem to matter, it's loss of info no matter where you're leaving to.

这篇关于单击浏览器的刷新按钮时如何弹出警告框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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