Javascript Alert Box Before Leaving Checkout

in Help edited November 2015
I'm trying to add a confirmation window when people try to leave the checkout page. It works great! ...except for the fact that it asks for confirmation even when they're checking out. I'm not very proficient in Javascript, but I know that I probably need a conditional statement that reads something like "if place order button is clicked, don't run."

Here's what I had put inside the checkout cart template:
<script type="text/javascript">
window.onbeforeunload = function () {
return 'Wait dont leave me text goes here';
}
</script>
Comments
  • fc_adamfc_adam FoxyCart Team
    @crystalimagesinc,

    Could you confirm for us which store version you're on? The solution will be slightly different depending on your version.
  • I'm on version 2.0
  • fc_adamfc_adam FoxyCart Team
    @crystalimagesinc,

    Thanks for confirming that. So you would make use of our new events in 2.0, which would allow you to set a variable that you could reference in your unload function - to check if a checkout is being attempted. That would look like this:
    {% if context == "checkout" %}
    <script>
    var checking_out = false;
    FC.client.on("checkout-submit-disable", function() { checking_out = true; });
    FC.client.on("checkout-submit-enable", function() { checking_out = false; });

    window.onbeforeunload = function () {
    if (!checking_out) {
    return 'Wait dont leave me text goes here';
    }
    }
    </script>
    {% endif %}
    That code should be added to the "footer" textarea of the "insert custom header and footer code" option, which you can find within the "cart" section of the "configuration" page of your store's FoxyCart administration. The code will only run on the checkout, and will only run the unload function if the customer isn't completing a checkout.
  • Alright, that works. Thanks so much!
Sign In or Register to comment.