Working With window.onbeforeunload

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Working With window.onbeforeunload

Post by icesolid »

I have a page that I have used the window.onbeforeunload code on. My only problem is that there is a "enlarge a photo" function used on the page that opens a little pop-up to blow up a photo. The window.onbeforeunload function works great however it wont let the pop-up window through. How can I make an exception for that enlarge() function, pop up window?

Here is what I am trying to do:

Code: Select all

if(windows is unloaded) {
    if(enlarge photo function was clicked) {
        do NOT display the "are you sure you want to navigate away" dialog box.
    } else {
         do display the "are you sure you want to navigate away" dialog box.
    }
} 
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Working With window.onbeforeunload

Post by Jonah Bron »

How about this?

Code: Select all

var isPopup = false;
window.onbeforeunload = function (){
    if (isPopup){
        // don't do whatever it does
    }else{
        // do the thing, because it's not the popup opening
    }
}
function openPopup(){
    isPopup = true;
    window.open(/* ... */);
}
Post Reply