Reputation: 369
I have the following javascript code using jquery 1.9.1:
(function ($) {
$(function () {
eventTracking();
});
function eventTracking() {
$("[data-formtracking]").bind('submit', function (e) {
e.preventDefault();
var eventName = $(this).data("formtracking");
rt.trackEvent(eventName);
$(this).unbind('submit').submit();
});
}})(jQuery);
Now, when I submit the form, it should make an update to a database, and then follow the action that is on the form, and basically redirect us to the other page.
In Chrome/IE the rt.trackEvent successfully fires and I get redirected to the other site. In Firefox, I get redirected to the other site, but no database entry.
If I add console.log between each statement, I can see that it is hits all of the log statements.
So at this point....I'm baffled. I've tried switching from bind to on/off. I've tried: $('input[type=submit]', this).click() based on a passing comment on another stackoverflow page.
A few notes: * rt is in an external file. I've checked and it does exist * If I remove the unbind.submit, then the rt.trackEvent works correctly, except the page doesn't redirect (obviously). * I've change the unbind.submit() to other variations, but then I have to click the button twice before it submits.
Thoughts?
Updated
The reason for the e.preventDefault() and later submission is that the form submission was happening too quickly. The page would redirect before rt.trackEvent would work properly. So I needed to slow the method down by adding a setTimeout($(this).unbind('submit').submit(), 2000);
I removed this to simplify the code, but in hindset it is pretty essential for the working of the code.
Updated 2 Here is the json that is being called:
options = $.extend({ success: $.noop, error: $.noop, urlBuilder: getNoCookieRequestURL, eventName: null }, options);
options.url = options.urlBuilder(options.url, options.siteId, options.eventName);
$.getJSON(options.url, function (jsonp) {
options.success(jsonp);
}).fail(function () {
options.error();
});
Updated 3
Change getJson to:
options = $.extend({ success: $.noop, error: $.noop, urlBuilder: getNoCookieRequestURL, eventName: null }, options);
options.url = options.urlBuilder(options.url, options.siteId, options.eventName);
$.ajax({
url: options.url,
dataType: 'json',
async: false,
success: function (jsonp) {
options.success(jsonp);
},
error: function() {
options.error();
}
});
I still seem to be having the speed issue. Code works when I step through it in Chrome, but database doesn't get updated properly when I just run it.
Upvotes: 0
Views: 3447
Reputation: 369
So not sure if this is the prettiest in the world, but it now works....which is a huge improvment.
$("[data-formtracking]").bind('submit', function (e) {
e.preventDefault();
form = this;
var eventName = $(this).data("formtracking");
rt.trackEvent(eventName);
setTimeout(function() {
form.submit();
}, 100);
});
Basically, the issue seemed to be with unbinding and resubmit using jquery, so we worked around that and used javascript's submit instead.
Upvotes: 0
Reputation: 95028
Don't unbind it, just submit it.
// $(this).unbind('submit').submit();
this.submit();
though, I wonder why you are preventing the submit in the first place if you're just going to submit it anyway.
(function ($) {
$(function () {
eventTracking();
});
function eventTracking() {
$("[data-formtracking]").bind('submit', function (e) {
//e.preventDefault();
var eventName = $(this).data("formtracking");
rt.trackEvent(eventName);
//$(this).unbind('submit').submit();
});
}})(jQuery);
Upvotes: 1