CMS Development

ballonura
Participant

Programmatically form redirect without jQuery (onFormSubmit)

SOLVE

There is anyway to do programmatically redirect the same form across multiple pages without require jQuery lib?

 

Like this solution just without jQuery:

https://community.hubspot.com/t5/Content-Strategy-SEO/Creating-a-download-or-e-book-download/td-p/35...

0 Upvotes
1 Accepted solution
stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

Programmatically form redirect without jQuery (onFormSubmit)

SOLVE

@ballonura sure, you can use vanilla js to programatically redirect a user to a new page.  Here's an example that checks if the user submitted the form with a phone number and redirects to different pages depending on that:

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
hbspt.forms.create({
portalId: "xxxxx",
formId: "xxx-xxx-xxx",
onFormSubmit: function() {
    var phone = document.querySelector('[name="tel"]').value;
    if(phone) {
       window.location.href = "http://exammple.com/ty-we-will-call-you";
    } else {
      window.location.href = "http://exammple.com/ty-we-will-email-you";
    }
  }
});
</script>

 

Stefen Phelps, Community Champion, Kelp Web Developer

View solution in original post

3 Replies 3
stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

Programmatically form redirect without jQuery (onFormSubmit)

SOLVE

@ballonura sure, you can use vanilla js to programatically redirect a user to a new page.  Here's an example that checks if the user submitted the form with a phone number and redirects to different pages depending on that:

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
hbspt.forms.create({
portalId: "xxxxx",
formId: "xxx-xxx-xxx",
onFormSubmit: function() {
    var phone = document.querySelector('[name="tel"]').value;
    if(phone) {
       window.location.href = "http://exammple.com/ty-we-will-call-you";
    } else {
      window.location.href = "http://exammple.com/ty-we-will-email-you";
    }
  }
});
</script>

 

Stefen Phelps, Community Champion, Kelp Web Developer
sharonlicari
Community Manager
Community Manager

Programmatically form redirect without jQuery (onFormSubmit)

SOLVE

Hey @ballonura 

 

thank you for sharing this. I'll tag a few experts that could help you.

 

Hey @stefen @SandyG1 @Jsum any thoughts you can share with @ballonura?

 

Thanks

Sharon


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !




0 Upvotes
ballonura
Participant

Programmatically form redirect without jQuery (onFormSubmit)

SOLVE

Thx @sharonlicari , i'll w8. Smiley Very Happy