+ Reply to Thread
Results 1 to 7 of 7

Thread: Javascript: Confirm a page exit

  1. #1
    krahny's Avatar
    krahny is offline x10Hosting Member krahny is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    25

    Question Javascript: Confirm a page exit

    I want to make a page that, when the user presses the exit button, a confirmation box appears that says "Are you sure you want to exit?". If you click OK, then it closes, and if you click Cancel it dosen't. Heres the code that I have:
    HTML Code:
    <body onunload="confirm('Are you sure you want to exit?')">
    When I exit with this code, though, it exits and then displays the confirmation box, witch is useless since the page has already closed.

    Could someone please help me figure out a way to stop the exit, and then exits if the user presses the OK button.
    Last edited by krahny; 12-29-2009 at 09:45 PM. Reason: I spelled a word wrong.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript: Confirm a page exit

    To cancel an event from an inline event handler, you need to return "false". Using DOM event registration, you'd call the event object's preventDefault method.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    krahny's Avatar
    krahny is offline x10Hosting Member krahny is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    25

    Re: Javascript: Confirm a page exit

    Quote Originally Posted by misson View Post
    To cancel an event from an inline event handler, you need to return "false". Using DOM event registration, you'd call the event object's preventDefault method.
    Could you please explain to me how to do this? Perhaps some code or something.

  4. #4
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Javascript: Confirm a page exit

    The browser will not allow you to control whether the page closes for security reasons. Therefore, there is a built in behavior within the unonload event.
    Code:
    function confirm() {
      //Code to execute before page close
      return '[close message]';
    }
    window.onunload=confirm;
    This displays a confirm box, sent by the browser, with the power to halt the page closing. Using DOM event registration leaves you to struggle between browser specific methods. This bit of code will work everywhere.
    Last edited by Twinkie; 12-30-2009 at 12:07 AM.

  5. #5
    krahny's Avatar
    krahny is offline x10Hosting Member krahny is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    25

    Re: Javascript: Confirm a page exit

    Quote Originally Posted by Twinkie View Post
    The browser will not allow you to control whether the page closes for security reasons. Therefore, there is a built in behavior within the unonload event.
    Code:
    function confirm() {
      //Code to execute before page close
      return '[close message]';
    }
    window.onunload=confirm;
    This displays a confirm box, sent by the browser, with the power to halt the page closing. Using DOM event registration leaves you to struggle between browser specific methods. This bit of code will work everywhere.
    I tried this code, but it didn't work either.

    Thanks anyway.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript: Confirm a page exit

    It turns out you need to use the beforeunload event, which works differently than other events. The event handler returns the string to display in the confirmation dialog. Basically, do what Twinkie shows, but assign confirm to window.onbeforeunload. Note that this event isn't a standard event, but it's supported by the current versions of most of the major browsers (Opera not included).
    Last edited by misson; 12-30-2009 at 05:58 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    krahny's Avatar
    krahny is offline x10Hosting Member krahny is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    25

    Talking Re: Javascript: Confirm a page exit

    It worked!

    Thanks!

+ Reply to Thread

Similar Threads

  1. Returning javascript with php
    By thenewprogrammer in forum Programming Help
    Replies: 5
    Last Post: 12-06-2009, 05:33 PM
  2. javascript text class change for heading
    By jakeselectronics in forum Programming Help
    Replies: 2
    Last Post: 10-17-2009, 12:31 PM
  3. Using Include Files with html on X10Hosting
    By frankfriend in forum Tutorials
    Replies: 0
    Last Post: 07-06-2009, 01:38 PM
  4. Replies: 3
    Last Post: 05-12-2009, 03:50 PM
  5. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers