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:
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