admin管理员组文章数量:1420073
On StackOverflow when you're asking a new question, you have enter the question and if you decide to navigate away from the page you get an "Are you sure" confirmation.
I'd like to do the same in my ASP.Net application:
The user has to fill in a questionnaire and has the option to store his answers temporarely. If the user decides to navigate away from the page without temporarely storing his answers we'd like a confirmation to popup and ask the user to store his answers.
Two questions:
What's a decent way of showing the confirmation popup before the page unloads in ASP.Net?
I'm aware of the beforeunload event, but I don't want to make it one big javascript hack.I don't want the confirmation to kick in when the user clicks the Save button (which is saving the answers anyway)
On StackOverflow when you're asking a new question, you have enter the question and if you decide to navigate away from the page you get an "Are you sure" confirmation.
I'd like to do the same in my ASP.Net application:
The user has to fill in a questionnaire and has the option to store his answers temporarely. If the user decides to navigate away from the page without temporarely storing his answers we'd like a confirmation to popup and ask the user to store his answers.
Two questions:
What's a decent way of showing the confirmation popup before the page unloads in ASP.Net?
I'm aware of the beforeunload event, but I don't want to make it one big javascript hack.I don't want the confirmation to kick in when the user clicks the Save button (which is saving the answers anyway)
3 Answers
Reset to default 4You have to write the action in
onbeforeunload Event
which fires prior to a page being unloaded.
<HTML>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.microsoft.">Click here to navigate to
www.microsoft.</a>
</body>
</html>
You should pay your attention on "onbeforunload" event As for the Save button you just can make some scripting logic, for example unsubscribe on this event or else.
You can try this:
<button onclick="javascript:doSend()">send</button>
<button onclick="window.xbuttons +='save ';">save</button>
<button onclick="window.xbuttons +='edit';">edit</button>
<script>
window.xbuttons = '';
window.onbeforeunload = function(){
if(!window.xbuttons.match(/save|edit/))
return "Do you want to leave this page?";
}
</script>
Here are the things you should note about onbeforeunload:
- onbeforeunload event will fire on every a (anchor elements) with href attribute
- onbeforeunload event will fire when the document location is change via javascript or by changing the url on the address bar
- onbeforeunload event will fire on any event that uses javascript: pseudo-protocol
本文标签: aspnetAsk for confirmation on page unloadStack Overflow
版权声明:本文标题:asp.net - Ask for confirmation on page unload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745328306a2653693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论