#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220

    If user tries to close the tab or the browser


    Hi;

    I am a JS retard. I was wondering if anyone would help me modify this so instead of mouse our of screen, I get the same pop up when the user tries to close the browser.

    Bootstrap Modal "Before you go" exit-popup

    I tested all these but none of them wok on closing the tab. Looks like there is something fishy about beforeunload.

    Thanks
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,332
    Rep Power
    9645
    What was the code you used and how did it not work? Did you also keep an eye on the Javascript console to see if there were any errors? Also, try testing the same code but with a different event that you can trigger while staying on the page (like a regular button onclick).
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Hey;

    It might be my browser but this is on and off. Not reliable. I tried variations of "onbeforeunload". None of them work!

    Can you please check this for me? I appreciate if you do:

    http://webmoosh.com/delete.html

    I can just close the tab and it don't pop up. If I can get the event to work, then I easily add a nice modal.
    Or this code simply should work, no?

    Code:
    window.onbeforeunload = function()
    	{
      		return 'Are you sure you want to leave?';
    	};
    Same with this:
    Code:
    <body onbeforeunload="return exit_pop();">
    Last edited by English Breakfast Tea; June 14th, 2016 at 02:38 AM.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    Jakarta, Indonesia.
    Posts
    279
    Rep Power
    36
    ...It might be my browser but this is on and off. Not reliable. I tried variations of "onbeforeunload". None of them work!...
    It is; see a yellow note in this ref:
    https://developer.mozilla.org/en-US/...onbeforeunload
  8. #5
  9. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Originally Posted by hdewantara
    It is; see a yellow note in this ref:
    https://developer.mozilla.org/en-US/...onbeforeunload
    Thanks for that. What do you recommend as alternative?
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    Jakarta, Indonesia.
    Posts
    279
    Rep Power
    36
    None, as it's not recommended to stop users from leaving. Sorry . But you could put small, kind note there on page which says:"Before you leave, please bla bla bla..."
  12. #7
  13. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Originally Posted by requinix
    What was the code you used and how did it not work? Did you also keep an eye on the Javascript console to see if there were any errors? Also, try testing the same code but with a different event that you can trigger while staying on the page (like a regular button onclick).
    Look you always say cool things (almost always) and takes me a few takes to understand it. At the end, I never understand you fully.

    How can I check js console to make sure window.onbeforeunload = function () is working fine?

    The window closes so the console dies with it.

    Thanks
  14. #8
  15. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Also requinix.... for the record, back in the day when I was doing coding for $, you saved my job (full time job) a few times. I love you for that, never had a chance to look in your eyes and show my appreciations.
  16. #9
  17. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,332
    Rep Power
    9645
    Originally Posted by English Breakfast Tea
    How can I check js console to make sure window.onbeforeunload = function () is working fine?
    You can't. You can test the code itself to make sure it works (by putting it in another event), or you could write code that does an AJAX call (so you can confirm through logs that the code executed), but otherwise I think you're stuck.

    Oh. Try setting a breakpoint at some code within the handler. The browser should respect that for navigation events and not try to navigate away until execution resumes, and it might even work when closing the tab/window too.
  18. #10
  19. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Originally Posted by requinix
    Oh. Try setting a breakpoint at some code within the handler.
    The browser should respect that for navigation events and not try to navigate away until execution resumes, and it might even work when closing the tab/window too.
    I dont wanna sound like a retard, but I am gonna. Please explain this if you can "Oh. Try setting a breakpoint at some code within the handler. "
  20. #11
  21. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,332
    Rep Power
    9645
    With Chrome and Firebug you can set Javascript breakpoints on lines of code. When the browser executes and reaches those lines, it will stop and you can inspect what's going on (like look at local variables). You might be able to use that same breakpoint stuff on code within your onbeforeunload handler to simply verify that the code is being called at all.

    Comments on this post

    • English Breakfast Tea agrees
  22. #12
  23. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Originally Posted by requinix
    With Chrome and Firebug you can set Javascript breakpoints on lines of code. When the browser executes and reaches those lines, it will stop and you can inspect what's going on (like look at local variables). You might be able to use that same breakpoint stuff on code within your onbeforeunload handler to simply verify that the code is being called at all.
    Marry me
  24. #13
  25. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Hey I am making progress. 2 things please help if you can.

    This seems to be working fine. How does it look to you? Missing anything?
    <script>
    window.onbeforeunload = function()
    {
    confirm_exit();
    };

    function confirm_exit()
    {
    alert('Show Bootstrap Modal');
    }
    </script>
    2 - About Firefox and Chrome, this guy is doing something that works and I can't figure what:

    Red Smoothie Detox Factor

    I read in a few places "If you interact with the page, FF allows onbeforeunload to be effective.

    Maybe he is doing something else that counts as interaction and so it gets effective? Maybe he does some ajax on $(document).ready(function() that labels the page as interactive?

    Ideas?

    Thanks
  26. #14
  27. Plays with fire
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    1,136
    Rep Power
    144
    Originally Posted by English Breakfast Tea
    I dont wanna sound like a retard, but I am gonna. Please explain this if you can "Oh. Try setting a breakpoint at some code within the handler. "
    Please stop using the word "retard."
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence

IMN logo majestic logo threadwatch logo seochat tools logo