SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stopping a Window from Closing

    i have a pop-up window with a form inside... if the user is filling out the form and on purpose/on accident closes the window, i'd like for a confirmation box to pop up asking if they really want to exit before finishing and then either continue to close the window, or keep it open.

    i'm assuming i need to place the script in the <body onunload="checkform();">, i'm just not sure what script to write to keep the window from closing.

  2. #2
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just check the result of the alert box and if it is cancel then return false from you event function. This should keep the window from closing.
    Wayne Luke
    ------------


  3. #3
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hate to be the bearer of bad tidings but there is NO 100% sure way to do this and even if you tried it will not work cross-browser .... this is one of the "age old" problems in web dev ..... most people handle this server side (IE: cleaning up sessions, removing data etc etc) ........ trying to do it client-side break almost every security rule in the book because you are attempting to manipulate their machine ........ here's some code to prove it.
    Code:
    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> New Document </title>
    </head>
    
    <body onunload="return confirm('are you sure you want to close me?');">
    
    </body>
    </html>

    the main problem is WHEN the onunload event fires ..... its literally AFTER the window close or they move to another page so in some browsers you actually get the confirm box AFTER the window is gone! (Neat huh)

  4. #4
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PS ---- this problem is EXACTLY why some of these scummy popups have NO frame at all ..... then the client CANT close them (and boy is that a pain!)

  5. #5
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try onbeforeunload and see http://www.webreference.com/dhtml/diner/beforeunload/

    And for closing pop-ups.. ALT+F4?

  6. #6
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now thats VERY cool encoded .... I can honestly say Ive never seen that before .... Ill have to remember that next time I need it!

  7. #7
    SitePoint Member TyeJae's Avatar
    Join Date
    Aug 2004
    Location
    Wyoming, Michigan
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is quite interesting...hehe
    TyeJae
    = $244


Bookmarks

Posting Permissions

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