• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

confirmation before leaving page

i've a situation, where users have to fill out a very lengthy form. and i want users get confirmation when they try to close the browser or browser tab or click any link on the page. but if they hit the submit or archive button they won't get that alert. any help comment or feedback would be deeply appreciated.


<p><a href="http://www.yahoo.com">link</a></p>

<form method="post" name="Form1" action="#">

<input type="text" name="fname" id="fname" size="32" />
<input type="text" name="lname" id="lname" size="32" />
<br />
<input type="submit" name="archive" id="archive" value="archive" />
<input type="submit" name="submit" id="submit" value="save" />

Open in new window

1 Solution
Test page : http://jsfiddle.net/ucWdB/
Want to learn ?
Here a good link : http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript
var submit_or_archive = false;

window.onbeforeunload = function() {
    if(!submit_or_archive) return "You're going to leave\nCancel to complete and\npost the form first!"

window.onload = function() {
    document.getElementById("mySubmitButton").onclick = function() {
        submit_or_archive = true;
    document.getElementById("myArchiveButton").onclick = function() {
        submit_or_archive = true;

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now