# Gravity form gform\_confirmation script is running multiple times

**URL:** <https://community.gravityforms.com/t/gravity-form-gform-confirmation-script-is-running-multiple-times/14620>\
**Category:** Get Help\
**Tags:** gform\_confirmation\_loaded, gform\_confirmation, confirmations\
**Created:** [March 3, 2023, 2:03pm UTC](https://community.gravityforms.com/t/gravity-form-gform-confirmation-script-is-running-multiple-times/14620 "2023-03-03T14:03:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shifa](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@shifa](https://community.gravityforms.com/u/shifa)\
**Post date:** [March 3, 2023, 2:03pm UTC](https://community.gravityforms.com/t/gravity-form-gform-confirmation-script-is-running-multiple-times/14620/1 "2023-03-03T14:03:53Z")

</div>

Hello, I have a issue with a form that’s inside a popup on a particular page .  
I have added a few lines of javascript through gform\_confirmation\_loaded function inside the gform\_confirmation filter. But my javascript is running multiple times on confirmation.

This is my code.

```auto
add_filter( 'gform_confirmation_7', 'custom_popup_confirmation_for_sgtracking', 10, 4 );
function custom_popup_confirmation_for_sgtracking( $confirmation, $form, $entry, $ajax ) {

if ( ! is_string( $confirmation ) ) {
return $confirmation;
}
$popupstatus = rgar( $entry, '4' );
$emailvalue = rgar( $entry, '1' );
$test_bug = 'test_bug';
if($popupstatus == "true"){

$confirmation .= GFCommon::get_inline_script_tag( "window.top.jQuery(document).on('gform_confirmation_loaded', function () {console.log('$test_bug /running'); } );" );

}
return $confirmation;

}

```

---

<div class="post-metadata">

**Author:** ![faisalahammad](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/faisalahammad/32/13948_2.png) [@faisalahammad](https://community.gravityforms.com/u/faisalahammad)\
**Post date:** [March 3, 2023, 4:19pm UTC](https://community.gravityforms.com/t/gravity-form-gform-confirmation-script-is-running-multiple-times/14620/2 "2023-03-03T16:19:49Z")

</div>

Your JavaScript code appears to be executed multiple times due to the `gform_confirmation_loaded` event firing more than once. This can occur if there are multiple Gravity Forms on the page or the form is being submitted multiple times.

To prevent this from happening, you can add a flag to your code that will only allow the JavaScript to be executed once. Here is an updated version of your code that includes a flag:

```auto
add_filter( 'gform_confirmation_7', 'custom_popup_confirmation_for_sgtracking', 10, 4 );

function custom_popup_confirmation_for_sgtracking( $confirmation, $form, $entry, $ajax ) {
    // If confirmation is not a string, return it unchanged
    if ( ! is_string( $confirmation ) ) {
        return $confirmation;
    }

    // Get the popupstatus and emailvalue from the form entry
    $popupstatus = rgar( $entry, '4' );
    $emailvalue = rgar( $entry, '1' );
    $test_bug = 'test_bug';

    // If popupstatus is "true", add JavaScript code to the confirmation message
    if($popupstatus == "true"){
        $confirmation .= GFCommon::get_inline_script_tag( "
            // Check if the flag is already set to prevent multiple executions
            if (window.top.popupConfirmationLoaded) {
                console.log('$test_bug /skipping');
            } else {
                // Set the flag to true to prevent further executions
                window.top.popupConfirmationLoaded = true;
                console.log('$test_bug /running');
            }
        " );
    }

    // Return the updated confirmation message
    return $confirmation;
}

```

In this updated code, we have added a check for the `window.top.popupConfirmationLoaded`. If this flag is already set to **true** , then we know that the JavaScript code has already been executed and can be skipped. If the flag is not set, we set it to true and execute the JavaScript code.

This should ensure the JavaScript code is only executed once, preventing further issues.

---

<div class="post-metadata">

**Author:** ![shifa](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@shifa](https://community.gravityforms.com/u/shifa)\
**Post date:** [March 4, 2023, 6:24am UTC](https://community.gravityforms.com/t/gravity-form-gform-confirmation-script-is-running-multiple-times/14620/3 "2023-03-04T06:24:39Z")

</div>

Thank you so much for your quick reply and easy solution. This works well 🥳. Thanks again.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [March 5, 2023, 6:25am UTC](https://community.gravityforms.com/t/gravity-form-gform-confirmation-script-is-running-multiple-times/14620/4 "2023-03-05T06:25:28Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
