• Hi,

    Are there any ways to prevent users/customers from exiting an important page?

    I’m building an eCommerce website for selling graphic design related services online.

    After checkout my customers get redirected to a form page to submit details regarding their order. Collecting these details is vital for me because I can not start working on the order without them.

    Are there any ways to prevent users from exiting the page? Is it possible to enable exit confirmation when a customer tries closing the page?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello there!

    You can achieve this with a custom plugin. Here’s a useful guide on creating one. Or, if you prefer to read a simplified version below:

    Create a plugin and call wp_enqueue_script to load your custom JS file.

    
    <?php
    /**
     * Prompt
     * Plugin Name: Prompt
     */
    function load_prompt_js() {
      wp_enqueue_script( 'Confirm Leaving', plugins_url( 'js/prompt.js', __FILE__ ), array('jquery'), '1.0.0', true );
    }
    
    add_action('wp_enqueue_scripts', 'load_prompt_js');
    

    Then, inside your js/prompt.js file, add the necessary checks.

    
    jQuery(document).ready(function($) {
      $(document).ready(function() {
          needToConfirm = true;
          window.onbeforeunload = askConfirm;
      });
    
      function askConfirm() {
          if (needToConfirm) {
              // Put your custom message here
              return "Your unsaved data will be lost.";
          }
      }
    });
    

    Remember to change the needToConfirm variable to false once the customer has successfully completed the order.

    Thread Starter jokubas2000k

    (@jokubas2000k)

    Hi @kevin940726,

    Thank you for you response. I’m sorry I took so long to reply.

    I followed the guide you provided and created the plugin and it kind of works.

    Here’s the issues:
    1. The popup only appears if the user has started filling out the form.
    2. The popup does not appear if the user has not answered any radio, checkbox, select etc. questions. Text input questions do not trigger the popup.

    Ideally, I would like the exit confirmation popup to appear even if the from is empty. Is that possible?

    I use the Forminator form plugin.

    Also,

    Remember to change the needToConfirm variable to false once the customer has successfully completed the order.

    How do I do that?

    The popup appears even when the user has finished and is submitting the form. How can I fix this?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to prevent users from exiting an important page?’ is closed to new replies.