How can I create a JavaScript warning when a user tries to leave the page?

Discussion in 'JavaScript' started by TwisterMc, Nov 3, 2008.

  1. #1
    I have a form that I'd like visitors to fill out. Once the form is started, I want a warning message to show when they try and leave the page and the form was not submitted. Something like "You have not submitted the form, are you sure you want to loose your information? [cancel] [ok]"

    I have gone though and done quite a bit of testing and I can get everything to work except, when the user chooses to stay, the page refreshes and all the form data is lost. :(

    Any ideas on how I can show a warning and not loose form data?

    Thanks
     
    TwisterMc, Nov 3, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use an onunload event. Here's a link with an example
     
    xlcho, Nov 4, 2008 IP
  3. proxywhereabouts

    proxywhereabouts Notable Member

    Messages:
    4,027
    Likes Received:
    110
    Best Answers:
    0
    Trophy Points:
    200
    #3
    Unfortunately, the page which you suggest here also doesn't add any value as when you close the page, it gve you "ok" "cancel". Click on cancel and still the page will close.
     
    proxywhereabouts, Nov 4, 2008 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
  5. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    TwisterMc, Nov 6, 2008 IP