Solved

Confirmation on window close

Posted on 2010-08-22
6
668 Views
Last Modified: 2012-06-21
Hi!

I have to show a confirmation dialog to user when he tries to close the window.
The user could navigate to others pages, the message should appears only in closing windows (or tab).

I tried this but it's alerts in navigation too:

<script language="javascript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
	return " Are you sure you want to exit this page?";
}
</script>

Open in new window


How can I alert the user only when he tries to close the window (or tab)?

Thanks in advance!
0
Comment
Question by:calypsoworld
6 Comments
 
LVL 3

Expert Comment

by:grantunwin
ID: 33496492

function showUnloading(evt){
evt.returnValue = "You are leaving.";
if (!confirm('Are you sure?'))
{
evt.preventDefault;
evt.stopPropagation; // this makes sure it does not bubble up
}

}
</script>

<body onbeforeunload="showUnloading(event); >
</body>

Open in new window

0
 
LVL 8

Expert Comment

by:Fareed Ali Khan
ID: 33496624
Hi,

I don't think you can stop the browser closing through JavaScript. Because of its different scenarios e.g. will it confirm from me when i end task the browser instance from task manager?

onBeforeunload event is called even on page refresh or the postback completes.

But you can show the dialog to user about browser is closing or perform some function calls at the time of browser is closing.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 33497371
About the best one could hope for is to rely upon a global var - call it OK - and set it's initial value to TRUE.

In your navigation functions, set OK to FALSE. Then in your "onbeforeunload" function, test OK. If it is false, then you are in charge of navigation, so do not display the alert. If it is TRUE, then the user is in charge, and the alert should be shown.
<script type="text/javascript">

var OK = true;

function showUnloading(e){
    if (OK) {
        e.returnValue = "You are leaving.";
        
        if (!confirm('Are you sure?')) {
            e.preventDefault;
            e.stopPropagation; // this makes sure it does not bubble up
        }
    }
}

window.onbeforeload = showUnloading;
</script>

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 29

Accepted Solution

by:
Badotz earned 500 total points
ID: 33497376
A slight change:


<script type="text/javascript">

var OK = true;

function showUnloading(e){
    if (OK) {
        e.returnValue = "You are leaving.";
        
        if (!confirm('Are you sure?')) {
            e.preventDefault;
            e.stopPropagation; // this makes sure it does not bubble up
        }
    }
    return false;
}

window.onbeforeload = showUnloading;
</script>

Open in new window

0
 

Author Closing Comment

by:calypsoworld
ID: 33500193
Thank you very much!
0
 
LVL 29

Expert Comment

by:Badotz
ID: 33500228
No worries - glad to help.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question