I'm trying to do a dynamic redirect on base one of my form fields.
I already look for a lot of ways of do that and I find out it would be posible if I add a event listener when form submit to get my field value and redirect my user to one page or another.
That javascript snippet should work on any site, whether it is a HubSpot website or landing page, or on a third party service. It adds a listener for when a HubSpot form is submitted and is independent of being on a landing page.
I set up a quick test in my portal, and added a form module and made sure it was set to "Display an inline thank you message". I then created a test page in HubSpot, and upon submit the eventListener previously posted printed a message to the console. The same worked in a custom module with a form field.
window.addEventListener('message', function (event) {
if (event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
/* print to the console on submit, or add in custom redirect in here */
console.log('onFormSubmit');
}
});
hi there can you share the code you are trying to run? I've used the provided example and I was able to create a redirection after form submission. But make sure in your form configuration you have only a message and not a redirect. I would recommend showing a message and adding a spinner with a timeout to trigger the redirection.
If this post helped you resolve your issue, please consider marking it as the solution. Thank you for supporting our HubSpot community.
I already tried a lot of ways and any of those worked for me.
I'm used to work with ReactJs and I tried to add a event listener on form submit and add a log to see if it works, but it never logs. Also I tried to get the element by name, id, tag but all of those returns a empty array. It looks like the code runs before the form is rendered.
Hi @BenSBM , Thank you for your answer and sorry for the delay to answer.
But how can I check the event using Hubspot Landing Page? Because I could check the event of onFormSubmit if i took the embeded code and paste on my website but i'm using hubspot landing pages.
That javascript snippet should work on any site, whether it is a HubSpot website or landing page, or on a third party service. It adds a listener for when a HubSpot form is submitted and is independent of being on a landing page.
I set up a quick test in my portal, and added a form module and made sure it was set to "Display an inline thank you message". I then created a test page in HubSpot, and upon submit the eventListener previously posted printed a message to the console. The same worked in a custom module with a form field.
window.addEventListener('message', function (event) {
if (event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
/* print to the console on submit, or add in custom redirect in here */
console.log('onFormSubmit');
}
});