asp.net - Ask for confirmation on page unload - Stack Overflow

On StackOverflow when you're asking a new question, you have enter the question and if you decide

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)

Share Improve this question edited Oct 12, 2009 at 9:20 Yvo asked Oct 12, 2009 at 9:03 YvoYvo 19.3k11 gold badges73 silver badges91 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You 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:

  1. onbeforeunload event will fire on every a (anchor elements) with href attribute
  2. onbeforeunload event will fire when the document location is change via javascript or by changing the url on the address bar
  3. onbeforeunload event will fire on any event that uses javascript: pseudo-protocol

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745328307a4622759.html

相关推荐

  • asp.net - Ask for confirmation on page unload - Stack Overflow

    On StackOverflow when you're asking a new question, you have enter the question and if you decide

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信