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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Detecting Browser close event in Javascript

    Hi...
    Is there anyway to differentiate between refreshing a window and closing the browser.
    It seems both events are handled by onUnload event.

    Thanks

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not in IE there isn't, since there's no way to trap how the event bubbles up the event hierarchy.

    you might be able to do this in Moz tho.

    however, there's no point doing it in one browser and not the other, especially when that browser is the marjority browser.

  • #3
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is it OK to use window.screenTop > 10000

    Hi...
    Some websites are using the following script to detect whether window is closed or not.
    if(window.screenTop > 10000)
    alert("Window is closed");
    else
    alert("Window stillOpen");

    It works fine in IE6.

    Can anyone clarify on the Magic number "10000".

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,949
    Thanks
    0
    Thanked 236 Times in 233 Posts
    This works in IE too.
    Code:
    function doUnload()
    {
     if (window.event.clientX < 0 && window.event.clientY < 0)
     {
       alert("Window is closing...");
     }
    }
    ...
    <body onunload="doUnload()">

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    neither methods worked for me!

  • #6
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    This works in IE too.
    Code:
    function doUnload()
    {
     if (window.event.clientX < 0 && window.event.clientY < 0)
     {
       alert("Window is closing...");
     }
    }
    ...
    <body onunload="doUnload()">
    But this code doesn't work if the mouse pointer is placed outside the window and top left corner.!!!!!!!!!!!!!!!!!!!!!!!!

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rajuds
    But this code doesn't work if the mouse pointer is placed outside the window and top left corner.!!!!!!!!!!!!!!!!!!!!!!!!
    that's because window.event detects mouse events (*states obvious*) so if the mouse is out of the window's boundaries, then naturally the event won't fire and the function can't be fully executed.

  • #8
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The function is still executing if the mouse is placed in the bottom righthand corner. This is b'coz

    The attributes window.event.clientX represents the location of mouse pointer from the starting coordinates of the browser window excluding toobar and menubar. So if the mouse pointer is positioned at top left corner then window.event.clientX is < 0 so the function id excuting.

  • #9
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rajuds
    if(window.screenTop > 10000)
    alert("Window is closed");
    else
    alert("Window stillOpen");
    this would only work with pop-ups tho. you couldn't run it on unload, and expect the "window is closed" alert to execute, because the window would be closed before it could kick in.

  • #10
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rajuds
    The function is still executing if the mouse is placed in the bottom righthand corner. This is b'coz

    The attributes window.event.clientX represents the location of mouse pointer from the starting coordinates of the browser window excluding toobar and menubar. So if the mouse pointer is positioned at top left corner then window.event.clientX is < 0 so the function id excuting.
    yes, of the window. not the screen. the user might not be using their mouse, might not even have one. if you presses alt+f4, the script won't execute at all.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,949
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by rajuds
    But this code doesn't work if the mouse pointer is placed outside the window and top left corner.!!!!!!!!!!!!!!!!!!!!!!!!
    What do you mean by this?

    Try this demo page. Probably step#6 is what you were talking about.
    This is tested in IE6 and it works. I don't know why it won't work for jbot.
    Code:
    <html>
    <head>
    <title>Detecting browser close in IE</title>
    <script type="text/javascript">
    function doUnload()
    {
     if (window.event.clientX < 0 && window.event.clientY < 0)
     {
       alert("Window is closed.");
     }
    }
    </script>
    </head>
    <body onunload="doUnload()">
    <p>
    Check which action detects browser window close:
    <ol>
    <li>Click this <a href="#" onclick="location.reload();return false">Refresh</a> link or the browser's Refresh button</li>
    <li>Navigate away from this page through a <a href="http://www.codingforums.com/">link</a></li>
    <li>Type another URL in the address bar</li>
    <li>Click Back or Forward button</li>
    <li>Click the Close (X) button in the top-rightmost corner of the browser</li>
    <li>Click the IE icon in the top-leftmost corner and choose Close. Or simply double-click the icon</li>
    <li>Press Alt+F4 key</li>
    </ol>
    </p>
    <p>In IE, the last 3 actions are correctly detected by the <a href="#" onclick="alert(doUnload);return false">javascript code</a> inside this page as browser close.</p>
    </body>
    </html>

  • #12
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    still doesn't work for me.

    i'm using IE 6.0.28 on Win-XPP.

    wot about you?
    Last edited by jbot; 04-22-2004 at 09:29 AM.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,949
    Thanks
    0
    Thanked 236 Times in 233 Posts
    IE 6.0.2800.1106 SP1 on NT Server

  • #14
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    interesting.

    i have noticed anomalies with IE before. my own company's website, for example, works fine on XPP but not on XPH.

    Could this be a security issue? That is, any security fixes for IE or XP might have turned this facility off in my version or on my machine. Would be worthwhile finding out.

    Kinda goes to show that you can never be too sure how accurate your code is, without ample development recoverage.

  • #15
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FWIW: It doesn't work for me either. (IE 6.0.2800.1106)

    .....Willy


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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