Link to home
Start Free TrialLog in
Avatar of hibbsusan
hibbsusan

asked on

forward/back buttons don't show confirm "are you sure you want to leave"

Hello!

A while back I asked if there were anyway to ask a user if he or she were sure they wanted to leave a page by showing an alert if anything but the confirm button were pressed. User leakim971  kindly posted this solution:

<input type="button" onclick="stopit=false;" value="Order" />

Open in new window


<script language="JavaScript">
var stopit = true;
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
if( stopit ) {
    return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
}
  }
</script>

Open in new window


It works really well, but I wonder if there is anyway to prevent showing the alert on browser "forward" / "back" button presses (?)

Thank you very much!
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hibbsusan
hibbsusan

ASKER

So is there no way to not show the alert when forward back buttons are pressed? Even with a new script?
No, that is a browser functionality object and not part of the rendered page. Sorry.
SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is there anyway to add a line to this code to make it scroll to the bottom of the page as well. To <a href="#confirmation"></a> ?
<a href="#confirmation"></a> will move the browser's scroll to focus the anchor or element with the ID "confirmation". Making that link scroll to the bottom as well defeats the purpose of having it navigate to "#confirmation".
I'm sorry...that was sort of redundant. I just want it to return the confirm box like it already does, and if the user decides to stay on the page, to scroll to the anchor #confirm
Does that make sense?

Thanks!

Leakim-
I will check out that article soon as I am back to a proper computer.
Thanks!