PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Javascript code in confirmation message is executed twice for Ajax form

  1. Anonymous
    Unregistered

    Hi,

    I use the "gform_confirmation" action to post submitted data to a third-party application. If the submission is successful, I need to include Javascript code in the confirmation message to track the conversion.

    For testing I have used just an alert("Conversion happened") box and noticed that it is shown twice for Ajax-enabled forms. I have not yet tested with the actual conversion code, but my guess is that 2 conversions will occur instead of one because of this behaviour.

    After several hours of debugging I have found out that this happens because the JS code is executed once inside the hidden <iframe> used as the target for Ajax-enabled forms and once when the confirmation message is added to the page (in GFFormDisplay::get_form() method - line 607 from "form_display.php", GF 1.7.5):

    "jQuery('#gform_wrapper_{$form_id}').replaceWith('<' + 'div id=\'gforms_confirmation_message\' class=\'gform_confirmation_message_{$form_id}\'' + '>' + confirmation_content + '<' + '/div' + '>');" .

    The solution I came up with was to remove <script> / <noscript> tags from the confirmation message before it is inserted in the page - this way scripts are executed only once inside the hidden <iframe>. For backwards compatibility, I made this configurable using "gform_confirmation_scripts_once" filter. Please see the unified diff file here:

    http://pastie.org/8040491

    Another possible issue is on line 604 from "form_display.php":

    "confirmation_content = contents;".

    where "contents" variable is set on line 585:

    "var contents = jQuery(this).contents().find('*').html();" .

    This will insert the entire content of the hidden <iframe>, including the:

    <head><meta charset='UTF-8' /></head>

    element inside the "#gforms_confirmation_message" <div>. In my opinion, only the <iframe> body content should be inserted in the page, so line 604 should be:

    "confirmation_content = jQuery(this).contents().find('.GF_AJAX_POSTBACK').html();".

    The diff file above includes all these changes. It would be nice if you could include them in the next GF version, if they seem correct to you.

    Kind regards,
    Eugen

    Posted 11 years ago on Thursday June 13, 2013 | Permalink
  2. Anonymous
    Unregistered

    Hi guys,

    Any update regarding these issues? Please let me know if there are some parts in the above post that are not clearly explained.

    Thank you in advance!
    Eugen

    Posted 11 years ago on Wednesday June 19, 2013 | Permalink
  3. Eugen,

    You would need to use the gform_confirmation_loaded hook instead:
    http://www.gravityhelp.com/documentation/page/Gform_confirmation_loaded

    Posted 11 years ago on Wednesday June 19, 2013 | Permalink