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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I generate an alert message when closing a browser?

    I can't figure this one out, so I need so help.

    I would like to have a "Are you sure?" alert box come up when the user tries to close their browser by clicking on the X or File/Close. The alert box needs to have 'yes' 'no' buttons. If they click 'no', the browser session doesn't close. I can get the alert box with unOnload, but the browser closes anyway.

    Thanks,
    ROBB

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    have a look at
    http://www.javascriptkit.com/javatutors/alert1.shtml

    that site has loads of good scripts and tutorials, and is run by the person (George aka WA) who runs these forums.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    IE-only:
    Code:
    <body onbeforeunload="return 'Are you sure?';">

  • #4
    Regular Coder
    Join Date
    Oct 2002
    Posts
    144
    Thanks
    12
    Thanked 0 Times in 0 Posts
    The tag "onbeforeunload" works by adding the text to the standard window close awrning popup.

    That leads to a further question. Can the Cancel button on the popup beroved forcing the user to answer "yes"?

    The Dingbat

  • #5
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hmmm...

    First of all I don't understand why do u use onbeforeunload rather than onunload.....
    Second....this is how it's done:
    <body onunload="return confirm('Are u sure?')">
    And as they say...happy coding

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    With onunload, the "unloading" of the page has already occurred, so returning false won't stop anything. That's why one must use onbeforeunload, as this event occurs *before* the actual unloading from the document happens.

    Also, the onbeforeunload event is different since it doesn't act like all the rest in the sense that returning a value is actually a part of a confirm asking whether or not the user wishes to unload the document. Returning the value of confirm() will actually call two confirms: the first one being the one defined by the scripter, and the second will say something like:

    Are you sure you want to navigate away from this page?

    (return value of confirm)

    Press OK to continue, or Cancel to stay on the page.
    Happy coding!
    Last edited by nolachrymose; 01-02-2004 at 02:17 PM.

  • #7
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh...dear me.....

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Of course, this will also come into play when the user tries to navigate away from the page... as well as when it's being closed.


  •  

    Posting Permissions

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