Jump to content

running clean up code when user leaves page without onbeforeunload dialog.


njdubois

Recommended Posts

I have a data entry form that has required fields.  I want to disable the ability for a user to browse away from this page until either these required fields are filled out, or they choose to cancel.

 

I found this on the net :

 

window.onbeforeunload = function() {

   return 'You have unsaved changes!';
}

 

But it is not going to do what I need it to do.  The user can still leave the page ignoring required fields, and not giving me the chance to delete the record.  Thus filling our database with bad data.

 

Ideally, The user starts filling out the form and for whatever reason needs to leave the page with the form, say they click the home link or maybe goes to google.com.  If any of the required fields are not filled out, I would like the code to prevent the page from changing, and to prompt the user using html and NOT the onbeforeunload dialog.  IE: Have some red text populate a div at the top of the page stating "A required field is missing, do you want to cancel this entry?" with a yes and no button.

 

Using the onbeforeunload event, I have no way to change the dialog other than the text in the message.  I also have no way of running code if they decide to leave the page.  I understand that for security reasons it has to be this way.  What are my options?  What would you do to get around this?

 

I'm really stuck against a wall with this, thanks for any help and thanks for even taking the time to read my post!

 

Nick

Link to comment
Share on other sites

 

window.onbeforeunload = function(){
if(!required_fields){ // do something to check if all fields are filled
var conf = confirm("You still have some required fields to fill out. Cancel?");
if(!confirm) return false;
else { // add a function that sends something to your database
}
}
};

 

Try something like that.

Link to comment
Share on other sites

var required_fields='true';
window.onbeforeunload = function(){
if(!required_fields){ // do something to check if all fields are filled
var conf = confirm("You still have some required fields to fill out. Cancel?");
if(!confirm) return false;
else { // add a function that sends something to your database
}
}
};

does nothing?  Whether is set it to true or false, it goes right to the chosen page, no prompts, nothing?

 

Based on what I have read, this isn't an option because you don't have control over whats going on inside the onbeforeunload event.

 

Thanks for the reply!  

 

Nick

Link to comment
Share on other sites

I want to disable the ability for a user to browse away from this page until either these required fields are filled out, or they choose to cancel.

You can't. The user can ALWAYS leave the page. There is no way at all for you to prevent that. There isn't even a reliable way to determine if the user has left the page.

 

What you need to do is setup your system so that until all these fields have been filled in, that DB record is considered to be incomplete and is more or less ignored. If you want, you can have a cron job periodically run to delete all the older incomplete records to conserve space.

 

Another alternative is to just not create the DB record at all until they have submitted all the necessary information. Store everything into session variables until everything is complete, then create the DB entry at the end.

Link to comment
Share on other sites

You can't. The user can ALWAYS leave the page. There is no way at all for you to prevent that. There isn't even a reliable way to determine if the user has left the page.

 

What you need to do is setup your system so that until all these fields have been filled in, that DB record is considered to be incomplete and is more or less ignored. If you want, you can have a cron job periodically run to delete all the older incomplete records to conserve space.

 

Another alternative is to just not create the DB record at all until they have submitted all the necessary information. Store everything into session variables until everything is complete, then create the DB entry at the end.

 

While reading your reply I had one of those 'DOH!' moments.  While I was aware that there was no way to prevent a user from leaving a page, I wasn't looking at it from the other direction.  Why not mark is as incomplete, and then output those records differently allowing the user to finish it later.

 

Thank you sooo much!!

 

Nick

Link to comment
Share on other sites

It really grinds my tits when a site tries to prevent me from leaving. One way trip to blacklist in my book. 

 

I agree with kicken, this is how I would do it:

 

Another alternative is to just not create the DB record at all until they have submitted all the necessary information. Store everything into session variables until everything is complete, then create the DB entry at the end.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.