Jump to content

Confirm page leave help


TitusRevised

Recommended Posts

The following code is supposed to give a confirmation popup when the user tries to leave the page the script is on. If he/she selects yes then it goes to the page they wanted to go if not i reloads the page.
My question is how do I make it so if they select yes it first directs to a php script which deletes information in the database and then directs them to the page. And if you can somehow get around it reloading if you select no and just close the popup box. Help is much appreciated.
[code]<script language="Javascript">
<!--

var your_message = "Are you sure you want to leave this page?";
var times = 0;

function onul() {
if (times == 0) {
  var leave = confirm(your_message);
  if (!leave) location = self.location;
  times++;
}
}

//-->
</script>
<body onunload="onul()">[/code]
Link to comment
Share on other sites

function onul() {
if (times == 0) {
  var leave = confirm(your_message);
  if (!leave) {
    location = self.location;
    times++;
  }
  else location = url('afile.php?refer=filename.html');
}
}

in afile.php do your database stuff then redirect to $_REQUEST['refer'], ie <?php header('Location:'.$_REQUEST['refer']);?>

Something like that, not sure if my code is exact but play around with it.
Not sure how to cancel the onunload event, its possible that if the function just returns false this might do it, but not sure, you'll have to try it...


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.