Link to home
Start Free TrialLog in
Avatar of dreamchaser23
dreamchaser23

asked on

Capture close button and prompt the user only when there is Unsaved Data in the Page

Read several solutions posted in EE so far regarding capturing close (X) button.
I tried them but they dont solve my problem:

<body onbeforeunload="HandleOnClose();"> //in the aspx page.

In the .js file:
function HandleOnClose()
    {
alert("UnsavedData");
           if(UnsavedData() == true)    
               {
                   count = count +1;
                  if (Count < 2 )
               
                     {
                          event.returnValue = "You have unsaved data.";
                      }

          }
         Else
            Count=0;
If I remove the alert from the code, the user is not prompted for unsaved data, when he clicks on close(X) button.
Please let me know what needs to be done to solve this.
If I have to further explain the problem, Ill be glad to do so.

Thanks a zillion for all your comments.
Regards
DreamChaser.
Avatar of dsacker
dsacker
Flag of United States of America image

Try using onunload in your body tag.
ASKER CERTIFIED SOLUTION
Avatar of dsacker
dsacker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dreamchaser23
dreamchaser23

ASKER

@dsacker: Unfortunately, onunload() doesnt work, friend.
I had tried it without any luck. After then, I stumbled upon onbeforeunload().

@bravebrain: Yeah UnsavedData() is a function.
I tried the code that you gave me. It prompts "true" and then displays the unsaved data prompt to the user.
I apologize, I typed this question in MS Word and by default, it changed else to "Else".

@dsacker: well return close() doesn't work as well, mate!!

Thanks a lot  for all your comments, friends!
Much appreciated.
Hopefully my issue gets resolved soon!
Lemme explain the scenario of the problem:

1.      User makes some change to the data
2.      Without saving it, he clicks on some other buttons. When this happens, HandleOnClose() function is called and he is prompted for unsaved data.
3.      a. Without saving the data, the user clicks on close (X) button, the page just closes without prompting the user.
3.      b. Without saving the data, user clicks on any button, and he is prompted for unsaved data. Now when he clicks on close (X) button, he is prompted for unsaved data, which is exactly how it should work.

If I am not clear, please let me know.
Thanks!
dreamchaser
Mods/Volunteers,
Though I am grateful for the comments for the experts,
Should I wait for some more time to get a solution that solves my problem or should I close this question and grade? It's gonna be 7 days.
Please let me know.
Thanks a lot!
Regards
DreamChaser
If you're looking for how to ask the user whether he wants to save or not:
function HandleOnClose() {
  if (confirm("You have unsaved data. Do you still want to close the window?")) {
    // OK button clicked; user still wants to close
    return true; // will close the window
  } else {
    // Cancel button clicked; save data.
    // return false; // uncomment this to cancel the close after saving data
  }
}

If that's not what you're asking I'm still lost ;o)
Hi Bravebrain,
Thank you again for your comment.
Assume I am calling this HandleOnClose function() in aspx page as:
<body onbeforeunload="HandleOnClose();"  >

In the javascript file, I have this code:
function HandleOnClose()  
 {
              if(DataChanged() == true)    
// alert("HandleOnclose"); //IF this alert message is uncommented, it doesn't prompt for unsaved data.

                         {
                   count = count +1;
                  if (count < 2 )
                      {
                          event.returnValue = "You have unsaved data.";
                      }
                     else if(count >1)
                     {
                              event.returnValue = "You have unsaved data.";                
                     }
   
                  else
                   count = 0;
           
               }
        }

We are pointing to the above javascript function, when some buttons in the page are clicked.
They prompt "Unsaved Data" error messages correctly when there is unsaved data.
Case 1:
a. Assume you made some changes to the data.
b. Assume you clicked on some button other than "Save" button, you get "Unsaved Data" alert message correctly, since you have unsaved data.
c. Now you can click either OK or CANCEL buttons.
d. Assume you clicked Cancel button, which means you are in the same page and you have unsaved data.
e. Now if you click on Close (X) button, the alert message for unsaved data will again appear correctly.

Case 2:
a. Assume you clicked on Close (X) button directly before clicking on other buttons when you have unsaved data, the page simply closes.

In other words, Close(x) button prompts up alert message only when some other alert message had already popped before in the page when the user clicked on some other button.

In further other words,
Close(X) button will close the page if it is the first one calling the HandleOnClose()function.
If someone/some control else had already called HandleOnClose() function, it will work perfectly.

I think if it is possible to make Close(X)button to call the button_click event (which is working correctly), we'll hit the solution.

I've tried my best to explain my situation. BraveBrain, if you aren't sure of anything particular in this comment, please let me know, I'll reply within 10 minutes.
Thank you so much Bravebrain.
Javascript Gurus, Geniuses, Sages please please throw some light on this issue.
I'll greatly appreciate any help from your side.

Thanks,
Regards
DreamChaser
Please disregard the above Javascript file and consider this one instead.
I removed the alert message because it is not desired at all.

function HandleOnClose()  
 {
              if(DataChanged() == true)    
                         {
                   count = count +1;
                  if (count < 2 )
                      {
                          event.returnValue = "You have unsaved data.";
                      }
                     else if(count >1)
                     {
                              event.returnValue = "You have unsaved data.";                
                     }
   
                  else
                   count = 0;
           
               }
        }

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry Bravebrain,
I didn't get a chance to work on this issue.
Had several different issues to handle.
Will try this weekend and keep you posted.
Thanks!!
Mods,
Please do not close this question for 5 more days.
Thanks a lot.
regards
DreamChaser