问题
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