View Full Version : Close method

12-07-2005, 05:04 AM
I want to trigger a function on close event. I tried with window.close().
But its not working can any one help on this

12-07-2005, 06:18 AM
I think you are looking for:


It's syntax is the same as the:


Meaning that if code is entered after the equals sign it needs to be as a function:

. . . load=function(){alert('Some Message');}

or if it is a function name, the parenthetical suffix is to be omitted:

function goodBye(){
alert('See Ya!');




The onunload event is not reliable cross browser to the extent that onload is and both of these events will not fire if javascript is disabled. So, don't use them for anything really important.

12-07-2005, 06:48 AM
Hi John,
Thanks for your reply. But there are many links in my page. If I click any of the link the page unloads and the function triggers. But I want that function only when I click the X (Close) button on the window.


12-07-2005, 08:52 AM
OK, basic onunload isn't the best for that and there really is no alternative I can think of for it that is too good. You see, the window close event triggered by clicking the x is not a javascript event. Some browsers will trigger onunload when it is clicked, others will not. For those that will, you can shield links from triggering it but not the backwards and forward buttons nor the simple act of typing in a new address and hitting 'go'.