刷新页面时如何显示提示信息 [英] How to show prompt message when refreshing the page

查看:65
本文介绍了刷新页面时如何显示提示信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Javascript,我有一个简单的页面.我需要在用户想要刷新页面时出现提示消息(F5,依此类推).有什么方法可以这样做吗?我曾尝试使用 method="post",但因为我没有在我的页面中提交任何内容,所以它根本不起作用.
很多时候用户想关闭提示信息,但是如果我想出现它怎么办?
警报消息?
有什么想法吗?

I work on Javascript and I have a simple page.I need to prompt message appear when user want to refresh the page(F5, so on). Is there any method to do so? I have tried to use method="post", but because I dont submit anything in my page it doesnt work at all.
Most of the time users want to disable prompt message, but what to do if I want to appear it?
Alert message?
Any ideas?

推荐答案

您可以使用 window.onbeforeunload.用户将收到您的消息,他有可能留下或离开.

You can use window.onbeforeunload. The user will be prompted your message and he has the possibility to stay or to leave.

JavaScript

window.onbeforeunload = function(e) {
    return "Don't leave";
};

演示

先试后买

了解更多关于 MDN window.onbeforeunload

Learn more on the MDN window.onbeforeunload

这篇关于刷新页面时如何显示提示信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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