Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

84 of 138 people (61%) answered Yes
Recently 6 of 10 people (60%) answered Yes


How to catch browser X button event. I have used onUnload in the body tag. It works fine but clickin
i'm having trouble in unloading a page. When i put an event in the unload event of a page it will ex

Apr 7th, 2008 22:32
ha mo, francis tudlong, drop zone, Colin Fraser, Mahesh Babu RCH,

there is a tricky method of catching the closing of a browser, thanks 
to pop up windows and the object.closed property.
all you have to do is open a small pop up window upon the onUnload
event and in the pop up ask a question about the opener window:
if (this.opener.closed)
    //means the opener window was closed
    //so you can do logout scripts here...
    //opener window wasnt closed so it means
    //either the user clicked "reload" or the
    //user changed pages (by click a link for ex).
thats it...
tricking the system, thats the name of the game :-)
The best answer I can give here is you cannot do what you want to do
witht he "X" button. 
Try to think of it like this, the ability of javascript is limited by
the constraints placed upon it by its designers. There are things that
it can do at different levels, like it can do a lot of things at teh
document level. It can do far fewer things at the window level, and it
can do fewer things at the browser level. This means you can capture
most tool bar items, but the exit/close button is fundamental to the
operation of the browser, and Javascript just does not work at that 
I am sure there will be some people who will disagree with this, but
that is OK, they can offer any explanation they like, essentially, it 
only at the document level, ie. the unLoad event specifically, that you
can have any hope of stopping the close of the browser. Test this by
using a script to  open a new window with all features turned off, all
attributes except size attributes, set to 0, look at what appears and
you will have no control over what is left.
I have some problem also in my code. I placed a code on the unload 
event of a page. When i clicked the (X) button of the browser it will 
popup the new window which is correct but when i tried to refresh the 
page why is it that it will popup the new window which is not supposed 
to be or when i click some links on the page. do you have some idea on 
how to open a popup window only if i press or click the (X) button of 
a browser such as IE.