-2

Possible Duplicate:
javascript detect browser close tab/close browser

I wrote a window closing function given below

window.onunload = function() {
    confirmBox();
}

function confirmBox(){     
    var con = confirm("Do you want to delete session and cookies?");
    if(con == true)
        logout();
}

There are three times the function called.

1) when refresing the page,
2) when go to another link within that site and
3) when the browser or tab will closed.

I just want the 3rd one. That is I want to call the window.close function or want to show the confirm box only when the tab or browser will closed.

Can anyone suggest how to accomplish this?

Community
  • 1
  • 1

1 Answers1

0

In order to make the 3rd apply, you need to make a workaround for the 1st and 2nd in your list. I don't think this is possible any other way.

The script should not apply when the script is loaded, so I don't know why that is happening.

A workaround for the link-issue could be to have a click-event that checks if you are just clicking in your document, or if the cursor is outside the window. If it is outside, the user is most likly trying to close the window, and the dialog should appear.

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96