Jason Kelly
Jason Kelly

Reputation: 2655

Prompting to save changes then confirm and close the window

I am attempting to write a function such that before the window is closed, it will check to see if my button is either in a disabled or enabled state which works fine, but for the second part of closing the window when I click on cancel, the window should stay open and not proceed to close. I am not sure as to where I am going wrong on this one. Thoughts?

//Detects and prompt for save changes before closing the window.
window.onbeforeunload = function() {

if ( confirm("Are you sure you want exit the application?") ) {

    if (!document.getElementById("save").disabled) {

        if (confirm("Do you want to save changes?") == true) {
            imts_save_changes()
        }
        else {
            return
        }

    }


}//end if
else { return }//keep the window open

}//end function 

Upvotes: 0

Views: 2241

Answers (1)

Voonic
Voonic

Reputation: 4785

onbeforeunload method itself shows a popup box for the string that is returned.

ex :

window.onbeforeunload = function() 
{
   return "Are you sure you want to exit";
}

This returned string will show up in pop up box.

However you can send asynchronous request before the return statement to interact with server before closing, but you cannot prevent closing of the browser

Upvotes: 1

Related Questions