CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2001
    Location
    Puerto Rico
    Posts
    26

    Question perform a javascript when a window is closed

    Hi everyone!
    I need to know how to perform some JavaScript code when a window is closed. Does anyone know how do I do that??

    I tried using unUnload on the bosy, but this execute the javascript everytime the page is unloaded (link is clicked, back button is pressed, fwd button is clicked... etc) and it doesnt helped me out. Is there anything as onClose??

    Please I need some help!!

    Thanks
    Jesy

  2. #2
    Join Date
    Jul 2002
    Location
    Don't Know, Don't Care
    Posts
    346
    well, window.closed is a boolean value that returns true or false. I've tried to tinker with it, but i think if you close the window that the code was issued from, it halts all scripting processes. So, i don't think you can use it in that way. But, you can use it on a child window.

    C G C F A D--Feel the Noise

    "When your life goes nowhere and leads back to me, doesn't that tell you something?"
    ~Gray Area Fury

  3. #3
    Join Date
    Aug 2002
    Location
    Reykjavik, Iceland
    Posts
    201
    For the cases where the onUnload is invoked by clicking on a link, you can simply set a flag for each link and then check the value of the flag on the onUnload event.

    The cases of forward and back is another story. Will this be in a popup window or the main browser window?

  4. #4
    Join Date
    May 2001
    Location
    Puerto Rico
    Posts
    26
    Thanks both for your replies...
    I tried using the window.closed but you were right waldo, it didnt work with a parent window.
    Anwering Websmith's question... my window is a parent window.
    About setting a flag in each link, it is difficult because my site is a big one ans it has too many links.

    My goal with all this is to sign off the user when he close the window... any suggestions are welcome.

    Thanks
    Jesy

  5. #5
    Join Date
    Jul 2002
    Location
    Don't Know, Don't Care
    Posts
    346
    well, you could you could launch your main site from another window that will blur when the child opens, then you could easily use window.closed()....i wonder if it works for frames as well....

    C G C F A D--Feel the Noise

    "When your life goes nowhere and leads back to me, doesn't that tell you something?"
    ~Gray Area Fury

  6. #6
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104
    According to the official(?) DOM reference at Mozilla, there is an onClose event for the window object in Netscape 6+, Mozilla and any other browser based on the Gecko engine. But my brief testing couldn't get it to fire.

    I also thought maybe you could do something with looking at the most recent window.history entry and comparing that to the current location... or something along those lines. But I couldn't get the history object to work at all in either Mozilla 1.1 or IE 5.5, so I don't have any specific suggestions in that regard.
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured