Jump to content

Confirmation when leaving a form

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
MPD Psycho

MPD Psycho

    Newbie

  • Members
  • PipPip
  • 21 posts
How do I write a JavaScript to show a confirmation window, when you made changes in a form field and for some reason you leave the form? You know like a pop-up window or something informing the user that one or more form fields have been changed, do you want to save or cancel?

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Use the alert() function to show a message (with just an OK button), and the confirm() function to show "OK" and "Cancel" buttons. confirm() and alert() both take one parameter - the text to show - and confirm() returns a boolean result reflecting the user's decision.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
MPD Psycho

MPD Psycho

    Newbie

  • Members
  • PipPip
  • 21 posts
I'm not a JavaScript expert. Can you show me a sample code or something?

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Neither am I! Sorry if I worded it badly. I think confirm() is the function to use. You pass in one parameter, the string, and it returns a boolean (true/false) value, depending on what the user clicked. For example:

var save = confirm('Going? Would you like to save?');
if (save)
{
  //Do code to save here.
}

var exit = confirm('Are you SURE you want to exit?');
if (exit)
{
  //Do code to exit.
}
else
{
  //If necessary, do code to stop exiting.
}

Two messages are displayed - first to save, then to confirm exit. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
MPD Psycho

MPD Psycho

    Newbie

  • Members
  • PipPip
  • 21 posts
Are those the same window with different messages (which is what I'm looking for) or two separated windows?

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
They are two different boxes that pop up one after another. Unfortunately, you cannot easily display more than one set of OK and Cancel buttons - you need one per message, so one per box. However, you could create a custom dialog box as an HTML page, then use window.open() to display it.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums