Skip to content

Instantly share code, notes, and snippets.

@axiak
Last active January 13, 2017 19:57
Show Gist options
  • Save axiak/2bf8f43d9d4a5f9c883f to your computer and use it in GitHub Desktop.
Save axiak/2bf8f43d9d4a5f9c883f to your computer and use it in GitHub Desktop.
/**
* Append the form data from a HubSpot form automatically
* to the redirect URL query parameters. These values can
* then be used on the form to modify the user experience
* of the Thank You page
*
* LICENSE
* Form redirect
* Written in 2015 by Mike Axiak <[email protected]>
* To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
* You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
(function() {
var $ = jQuery;
var appendFields = function (url, values) {
var data = {};
$.each(values, function (i, item) {
if (item.name !== "hs_context") {
data[item.name] = item.value;
}
});
if (url.match(/\?/)) {
return url + "&" + $.param(data);
} else {
return url + "?" + $.param(data);
}
};
$(function () {
$("body").on("submit", "form.hs-form", function (e) {
var $form = $(this);
var apiUrl = $form.attr("action");
var $hsContext = $("input[name='hs_context']", $form);
var hsContext = JSON.parse($hsContext.val());
hsContext.redirectUrl = appendFields(hsContext.redirectUrl, $form.serializeArray());
$hsContext.val(JSON.stringify(hsContext));
});
});
})();
@mayashavin
Copy link

mayashavin commented Nov 3, 2016

+1 hsContext.redirectUrl is undefined in my case also. If I defined redirectUrl when I called hbspt.forms.create({}) then it is ok, otherwise I can't see the default redirectUrl

@flexgrip
Copy link

I updated this to work with the new hubspot forms js. It sucks that you have to manually define the redirect now. But what are you gonna do?

https://gist.github.com/flexgrip/0ef20e183ca7fe577b5d309620fc44bc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment