Results 1 to 4 of 4

Thread: Close method

  1. #1
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Close method

    Hi,
    I want to trigger a function on close event. I tried with window.close().
    But its not working can any one help on this

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think you are looking for:

    Code:
    window.onunload=
    It's syntax is the same as the:

    Code:
    window.onload=
    Meaning that if code is entered after the equals sign it needs to be as a function:

    Code:
    . . . load=function(){alert('Some Message');}
    or if it is a function name, the parenthetical suffix is to be omitted:

    Code:
    function goodBye(){
    alert('See Ya!');
    }
    
    window.onunload=goodBye;
    not:

    Code:
    window.onunload=goodBye();
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

    Regards,
    Mike.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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'.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •