Attaching js to form submit

I have a problem with a js function not being fired that has been attached to a form submit.

The script essentially calls a tracking script when the submit button is clicked. It appears that the submit handler completes faster than the tracking script has time to fire.

I have attached the script attached to the form submit

(function ($) {     Drupal.behaviors.MYTRACKINGSCRIPT = {       attach: function(context) {           $('.entityform').submit( function(e) {             var form = this;             var question_number = $('.question-number em:first-child').text();             var question_name = $('.title-wrapper').find('h2').text().replace(/\s/g, '_').toLowerCase();             var f24_question = question_name + '-' + question_number;             var fields = $(".l-content :input:not([type=hidden])").serializeArray();             f24_answer = '';             jQuery.each( fields, function( i, field ) {               if(f24_answer){                 f24_answer = f24_answer + ', ' + field.value;               }else{                 f24_answer = field.value               }              });             var url = window.location.href;             f24_trackValue(f24_question, f24_answer);             f24_trackPage(url + '#' + f24_question);           }       )     }   } })(jQuery); 

This is the third party script which the functions are being called from

//Copyright 2010-2015 Force24 //F24 Tracking V1.4.0 //****** Config Settings ******  var f24_customerId = '6a3cee44-7c96-e411-85e0-0050568c0022'; var f24_fullTrackingURL = ''; var f24_details = new Array();  if(undefined === window.f24_initialPageTracked){     window.f24_initialPageTracked = false; }  //***************************** function f24_getQueryParam(name) {     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");     var regexS = "[\\?&]" + name + "=([^&#]*)";     var regex = new RegExp(regexS);     var results = regex.exec(window.location.href);     if (results == null)         return "";     else         return results[1]; } function f24_getCookie(name) {     if (document.cookie.length > 0) {         start = document.cookie.indexOf(name + "=");         if (start != -1) {             start = start + name.length + 1;             end = document.cookie.indexOf(";", start);             if (end == -1) end = document.cookie.length;             return unescape(document.cookie.substring(start, end));         }     }     return ""; } function f24_setCookie(name, value, exdays) {     var exdate = new Date();     exdate.setDate(exdate.getDate() + exdays);     document.cookie = name + "=" + escape(value) + ((exdays == null) ? "" : ";expires=" + exdate.toUTCString()) + "; path=/"; } function f24_trackPage(page) {     var f_personId = f24_getCookie('f24_personId');     var f_autoId = f24_getCookie('f24_autoId');     var pLoaded = false;     var counter = 0;      if ((typeof f_personId != 'undefined') && (f_personId != 'undefined') && (f_personId != '')) {         var f_referrer = 'UnknownReferrer';         if (document.referrer) {             f_referrer = document.referrer;             if ((typeof f_referrer == 'undefined') || (f_referrer == 'undefined') || (f_referrer == '')) {                 f_referrer = 'UnknownReferrer';             }             f_referrer = encodeURIComponent(f_referrer);         }          if(page) {             page = encodeURIComponent(page);          }          var tp = new Array();         tp.push('cId=' + f24_customerId);         tp.push('pId=' + f_personId);         tp.push('aId=' + f_autoId);         tp.push('loc=' + page);         tp.push('ref=' + f_referrer);          for (var i = 0; i < f24_details.length; i++)         {             tp.push(encodeURIComponent(f24_details[i].key) + '=' + encodeURIComponent(f24_details[i].value));         }          var imgurl = f24_fullTrackingURL + '?' + tp.join("&");          ti = new Image(1, 1);         ti.src = imgurl;         ti.onload = function () { return; };     } } function f24_trackPageView() {     var f_location = 'UnknownLocation';     if (document.location) {         f_location = encodeURIComponent(document.location);     }     f24_trackPage(f_location); } function f24_trackPageViewInit() {     if (!window.f24_initialPageTracked)     {         window.f24_initialPageTracked = true;         f24_trackPageView();     } } function f24_clearValues() {     f24_details = new Array(); } function f24_trackValue(key, value) {     if (!key)         return;     if (!value)         value = "";     f24_details.push({ key: key, value: value }); } function _f24Track(key, value) {     f24_trackValue(key, value); } //****** Init code *************** var f24_new_personId = f24_getQueryParam('f24_pid'); var f24_autoId=f24_getQueryParam('f24_autoid'); if ((typeof f24_new_personId != 'undefined') && (f24_new_personId != 'undefined') && (f24_new_personId != '')) {     f24_setCookie('f24_personId', f24_new_personId, 3650); } else if((typeof f24_autoId != 'undefined') && (f24_autoId != 'undefined') && (f24_autoId != '')) {     f24_setCookie('f24_personId', f24_autoId, 3650);     f24_setCookie('f24_autoId', f24_autoId, 3650); } if (document.readyState === "complete") {     f24_trackPageViewInit(); } else {     window.addEventListener ? window.addEventListener('load', f24_trackPageViewInit, false) : window.attachEvent('onload', f24_trackPageViewInit); } 

The form being submitted is a custom multipage form with one question per page and it does a full page refresh on submission of each page. The values are tracked if the form is completed quickly but if you pause for a minute between questions then the tracking function doesn't appear to fire.

Maybe I need to add a delay.

Cheers Dan


Category: forms Time: 2016-07-28 Views: 0

Related post

  • How can i reload jQuery scripts after form submit in views using ajax? 2012-03-03

    I have a views with ajax enabled using B.E.F. to filter some content. After submitting these filters jquery scripts stop working. Please help me to find an answer how to reload any of these scripts after using submit button with ajax? I`ve tried to d

  • Prevent "AJAX HTTP request terminated" error on early form submit 2012-12-12

    I'm doing an ajax validation for duplicate check on link field in node form. When the link field is edited, an ajax call is triggered as programmed (using form api #ajax default blur event), it takes a while for the ajax response to reach browser, in

  • Can a form submit handler return results without using the $_GET array? 2012-12-17

    It looks like the default method for passing the result of handling a form submit is GET. The following statement will redirect to form/result, where a page callback function will extract $content (the result form processing the form) from the query

  • form submit function never gets called. 2013-04-24

    My drupal form submit function is not called. validation function is called. Form name is Mymodule_user_profile_form($form, &$form_state), validation function name is Mymodule_user_profile_form_validate($form, &$form_state) and submit function nam

  • How to add a universal form submit handler to ALL forms? 2013-07-08

    Suppose i need to record all form submissions in a site, i mean all of them. I would add a new submit handler to be executed after each forms submit handler, like $form['#submit'][] = 'log_submit' , right? The problem is that some forms may not have

  • Fire javascript on a form submit 2013-10-02

    Even the searching I have done that looks like there may be a solution doesn't really seem to answer this. I have a form with an input value. When the user enters a value and clicks submit, I want to call a javascript routine and make that value avai

  • How to reload page after ajax form submit 2014-04-04

    I'm using FAPI #ajax to make the form more user friendly (it reloads in-place on validation). Works like a charm. However I would like to reload the page after form is submitted. How to that? Obviously if I call for example drupal_goto(current_path()

  • Views Exposed Filter Form Submit handler 2014-06-18

    I have a Views with an exposed filter block and put the block on the front page. I use the following code to add a custom submit handler. function mymodule_form_alter(&$form, &$form_state, $form_id) { if($form_id == 'views_exposed_form' ) { array_

  • Upload image temporary and on form submit make it permanent 2014-08-14

    I have made a custom module where I load the create node form for an "announcement" content type. What i want Besides the text and select fields, I require an image field which supports multiple image upload (up to 4 ). The images should be uplo

  • Saving a value from Javascript on Drupal form submit 2014-08-18

    I have a jQuery map that I've built that allows users to select states and counties. This is all done in Javascript. I would like to, on form submit, take this data and submit it to the user's profile2. Should I create an empty form element and fill

  • Opening a ctools modal on form submit 2014-12-20

    I have a custom profile form (part of a multi-step registration process) that requires a modal to be displayed once submitted. I am not clear on what the process is for this to happen, and the documentation i've seen isn't clear. What I have tried (a

  • How to Rebuild Registry On Admin Form Submit 2015-01-03

    I would like to initiate theme registry rebuild(drupal_theme_rebuild) on a particular form submit. This is admin form for my custom settings: function HOOK_menu(){ $items['admin/some-url'] = array( 'title' => 'Custom Settigns', 'page callback' => 'd

  • jQuery Prevent Multiple Form Submit 2011-03-30

    It might be useful on some forms to prevent multiple submits on a form by accident. This code binds the submit event and stores disabledOnSubmit" data so next time the event "submit" fires it will return false. All submit buttons are also d

  • Tracking user contact form submissions 2011-05-24

    How can I track user contact form submissions on Drupal 6? I'm thinking of something similar to the webform results table but for user contact forms. If not something that complex, then just simple statistics on contact form use, e.g. "UserX received

  • Attaching the comment form programmatically 2011-05-29

    How do I add the comments form (of the comments module) programmatically in a custom-made module? In other words, what proper code should be added to your module code to add a complete "Post New Comment" form (of the Comments core module)? That

  • Ajax on form submit help 2011-07-10

    This works if you actually click the button: $form['submit'] = array( '#type' => 'image_button', '#src' => drupal_get_path('module', 'contact_block') . '/images/send_button.png', '#ajax' => array( 'callback' => 'contact_block_ajax', 'method' =

  • call javascript function after ajax form submit 2011-07-12

    How do I call a javascript function after a successful ajax form submit? I want to call a function in a file that I added with drupal_add_js() in my_form_name_form($form, &$form_state). --------------Solutions------------- Have a look at the Ajax fra

  • How to pass value generated in the form submit function to another function? 2011-07-12

    I have a question related to drupal 7 form api and module development. I have a very simple module with a function like this. It gets called by hook_menu when a certain path xyz is visited. function myview_mainpage() { $content .= drupal_render(drupa

  • Public posts - preventing duplicate form submissions 2011-07-29

    I'm creating a public form on a page that allows non-registered (public) visitors to create posts of a custom post type. I'm using nonces, which I naiveley thought would expire once they were used. This doesn't appear to be the case. This post seems

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.903 (s). 13 q(s)