How to show prompt message when refreshing the page [duplicate]

早过忘川 提交于 2021-02-11 14:18:19

问题


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?


回答1:


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";
};

Demo

Try before buy

Learn more on the MDN window.onbeforeunload



来源:https://stackoverflow.com/questions/18704177/how-to-show-prompt-message-when-refreshing-the-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!