Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Closing Window

  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Closing Window

    May I know which Javascript function detects when user press the X (close) button? I tried using the onbeforeunload event but seems like this works when user clicks the Back, Forward, Refresh, Close Window or Home button.

    I want it to be only for the X (Close Window) button. How can I do so?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    There is really no cross-browser solution to detect when the user presses the X button. There is a trick in IE but you can only detect pressing it but you cannot cancel closing the browser like onbeforeunload does. BTW, what's your ultimate reason for detecting the closing of browser? There may be better solution for that.

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I am doing a chat program and when user closes the main window by pressing the X button, it will prompt user if he wants to log out or not. That's my main purpose. How can I do this then?

  • #4
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 10 Times in 10 Posts
    I have seen something do that before however I forget where I've seen it.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    function doUnload()
    {
     if ((window.event.clientX < 0) && (window.event.clientY < 0))
     {
        //open the logout page that auto-closes
        window.open("logout.xxx", "", "width=200, height=20");
     }
    }
    window.onunload = doUnload;
    As I said, this is for IE only and must be used with caution as this can be annoying to some users.


  •  

    Posting Permissions

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