www.webdeveloper.com
Results 1 to 5 of 5

Thread: Confirm Close of Window

  1. #1
    Join Date
    Feb 2006
    Posts
    169

    Confirm Close of Window

    I am trying to come up with a function that will confirm the user actually wants to close the browser window.

    It is easy to do if I create the close button but I am trying to have it work if someone closes the window another in other ways. For example the X button. Here is were I am with it. The confirm works but if they click Cancel the window closes anyway. Can anyone steer me in the right direction?


    Code:
    function init() {
        if (confirm("You have not submitted your registration yet.  Are you sure you want to quit?")) {
          parent.close();
        }
        else
        {
        alert("Close cancelled.");
        }
    }
    window.onunload = init;

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You can not close, using javascript methods, a window which was not previously opened on using javascript methods. Is it your case?

  3. #3
    Join Date
    Feb 2006
    Posts
    169
    I have a registration site where users are closing their browser using the X button (top right of IE) on the review page. I want to alert them if they do this that they are registration has not been submitted until they click continue to the confirmation.

    Basically I want to fire off a confirm when the user closes their browser which i can successfully do with the above function. The problem is, if they click cancel on this confirm alert it closes the brower anyway.

  4. #4
    Join Date
    Aug 2007
    Posts
    3,767
    You need window.onbeforeunload.
    Code:
    window.onbeforeunload = function () {
    return "If you leave now, your registration will be canceled.";
    };
    Great wit and madness are near allied, and fine a line their bounds divide.

  5. #5
    Join Date
    Feb 2006
    Posts
    169
    Thanks works perfect

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles