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 4 of 4
  1. #1
    Regular Coder Bengal313's Avatar
    Join Date
    Jun 2002
    Location
    Metro Detroit
    Posts
    298
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Dialog box on close event

    How can I create a dialog box before closing the web browser (IE or Firefox). When a user clicks on the "X" on the upper right hand I need a dialog box to appear and display a message "Have you logged out first? " Yes or No.

    Yes Button - Close the browser window and dialog box
    No Button - Don't close browser. Close dialog box

    Any idea how?
    "Knowledge Speaks But Wisdom Listens"

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    <script type = "text/javascript">
    window.onbeforeunload = ask;
    function ask() {
    return "";
    }
    </script>

    But onbeforeunload only works in IE and Firefox.

    For those browsers that don't support it, it will do nothing.
    Last edited by Philip M; 12-21-2007 at 07:04 PM.

  • #3
    Regular Coder Bengal313's Avatar
    Join Date
    Jun 2002
    Location
    Metro Detroit
    Posts
    298
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Great. It does what I want. But can I customize the message? Something like,

    Have you logged off IDC? Yes or No

    If yes close window
    If no remain open
    "Knowledge Speaks But Wisdom Listens"

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    confirm('any text you want goes here')

    will display two buttons - OK and Cancel for your visitor to choose between.

    Just remember that the onbeforeunload event is non-standard and only works in IE and Firefox and only if the browser owner has not disabled it (as any sensible person would in order to stop sites from being able to interfere with their ability to leave).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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