# JS File Not Triggering Delay After Submitting Form

**URL:** <https://community.hubspot.com/t/js-file-not-triggering-delay-after-submitting-form/21986>\
**Category:** CMS Development\
**Tags:** javascript\
**Created:** [June 23, 2020, 3:16pm UTC](https://community.hubspot.com/t/js-file-not-triggering-delay-after-submitting-form/21986 "2020-06-23T15:16:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alejandro/32/8032_2.png) [@Alejandro](https://community.hubspot.com/u/Alejandro)\
**Post date:** [June 23, 2020, 3:16pm UTC](https://community.hubspot.com/t/js-file-not-triggering-delay-after-submitting-form/21986/1 "2020-06-23T15:16:10Z")

</div>

Hello,

I posted this question in the Marketing Hub community, but that might’ve been the wrong place to ask this question so I’m trying my luck here.

I’ve created a JS file in the Design Manager tool and am trying to attach it to a landing page within the Header/Footer HTML section, but it seems that the landing page isn’t accepting it.

What I’m trying to do, is to add a delay to the page so when the form is submitted, the Thank You page is delayed long enough for the contact to be created and a dynamic property on the Thank You page is populated with the information given.

Here is the JS:

```auto
<script>
jQuery.getScript("//js.hsforms.net/forms/v2.js", function() {
 hbspt.forms.create({
 portalId: "XXXX",
 formId: "XXXXXXXXXXXXXXXXXXX",
 onFormSubmit: function($form) {
 setTimeout(function() {
 window.location = "{{ thank-you-page-link }}";
 }, 50000);
 }
 });
});
</script>

```

The landing page template itself is an HTML file and not a WYSIWYG-style template, so there’s no area that I can add the JS file. I’ve tried adding it to both the Head and Footer of the landing page builder with the following:

```auto
<!-- start Main.js Do Not Remove -->
<script src='{{ public_URL }}'></script>
<!---End Main.js-->

```

As of now, I’m stuck; does anyone out there have an idea of what I can do to get this thing to start working?

---

<div class="post-metadata">

**Author:** ![sharonlicari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sharonlicari/32/101479_2.png) [@sharonlicari](https://community.hubspot.com/u/sharonlicari)\
**Post date:** [June 26, 2020, 10:12am UTC](https://community.hubspot.com/t/js-file-not-triggering-delay-after-submitting-form/21986/2 "2020-06-26T10:12:05Z")

</div>

Hey [@Alejandro\_](https://community.hubspot.com/t5/user/viewprofilepage/user-id/115716)

Thank you for the information provided.I’ll tag a few experts that can share their experience with you.

Hey @daveroma @BenSBM [@david\_eic](https://community.hubspot.com/t5/user/viewprofilepage/user-id/67318) any thoughts you would like to share with [@Alejandro\_](https://community.hubspot.com/t5/user/viewprofilepage/user-id/115716) ?

Thanks & Happy Friday

Sharon

---

<div class="post-metadata">

**Author:** ![daveroma](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/daveroma/32/82727_2.png) [@daveroma](https://community.hubspot.com/u/daveroma)\
**Post date:** [June 26, 2020, 12:43pm UTC](https://community.hubspot.com/t/js-file-not-triggering-delay-after-submitting-form/21986/3 "2020-06-26T12:43:49Z")

</div>

Hi [@Alejandro\_](https://community.hubspot.com/t5/user/viewprofilepage/user-id/115716) - CC @sharonlicari

First check the Network tab to make sure your JS file is loading:

 ![28600i78EFABF291727F3E.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/4/c/4ca0af12379207ba6c4e090181b1b2451abf43f4.png)

Once you’re sure the JS file containing your script is being loaded onto the page - you’ll want to wrap your script in jQuery’s document ready function which will ensure the script fires once the DOM is completely loaded - see below:

```auto
<script>$(function() {

 console.log('The DOM is ready!');

 jQuery.getScript("//js.hsforms.net/forms/v2.js", function() {
 hbspt.forms.create({
 portalId: "XXXX",
 formId: "XXXXXXXXXXXXXXXXXXX",
 onFormSubmit: function($form) {
 setTimeout(function() {
 window.location = "{{ thank-you-page-link }}";
 }, 50000);
 }
 });
 });
});</script>

```

I added a console.log() in there so you can open dev console and if you see a console log message you’ll know the script is actually firing.

Let me know how you make out.

---

<div class="post-metadata">

**Author:** ![Alejandro](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alejandro/32/8032_2.png) [@Alejandro](https://community.hubspot.com/u/Alejandro)\
**Post date:** [June 26, 2020, 9:12pm UTC](https://community.hubspot.com/t/js-file-not-triggering-delay-after-submitting-form/21986/4 "2020-06-26T21:12:01Z")

</div>

@daveroma - This is frustrating. The console log shows up so that means the script is firing… but anywhere that I stick the JS doesn’t seem to work.

I’ve tried adding the JS file to the form module in the Linked files section; no dice.

I’ve tried adding the code as you have it below to the module.js section of the form module; no dice.

I’ve created a custom module with only adding the code to the module.js section and adding the usage snippet to the HTML template right below the \<body\> tag; no dice.

I’ve even tried adding it to the landing page itself in the builder in the Footer section using the public URL for the JS file; no dice.

I don’t know what to do but I feel like I’m missing something that’s letting this work because it’s seemingly a simple thing to do - add a delay when submitting a form.

I really appreciate the help with this. I don’t think I’ve been this frustrated since watching the last episode of Lost.

---

<div class="post-metadata">

**Author:** ![Alejandro](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alejandro/32/8032_2.png) [@Alejandro](https://community.hubspot.com/u/Alejandro)\
**Post date:** [June 26, 2020, 11:21pm UTC](https://community.hubspot.com/t/js-file-not-triggering-delay-after-submitting-form/21986/5 "2020-06-26T23:21:51Z")

</div>

I believe I’ve solved it!

I ended creating a seperate module and adding the code you gave me, @daveroma, (with a slight tweak) to the module.js section (thank you for the inspiration, @WendyGoh!), copy-and-pasted the usage snippet code right underneath the \<body\> tag and - BAM - it worked!

The only difference is that I removed the redirectURL in the form and added an inline message. So now, instead of a delay right after the submit, the inline message appears and the new JS fires to the thank you page after a slight delay.

Here’s the slightly adjusted code (replaced the onFormSubmit with onFormSubmitted):

```auto
$(function() {

 console.log('The DOM is ready!');

 jQuery.getScript("//js.hsforms.net/forms/v2.js", function() {
 hbspt.forms.create({
 portalId: "XXXXXXX",
 formId: "XXXXXXXXXXXXXXXXXXXXXXXX",
 onFormSubmitted: function($form) {
 setTimeout(function() {
 window.location = "{{ your-link-here }}";
 }, 2000);
 }
 });
 });
});

```

Again, thank you all for your help; I honestly couldn’t have done this without y’all!

---

<div class="post-metadata">

**Author:** ![daveroma](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/daveroma/32/82727_2.png) [@daveroma](https://community.hubspot.com/u/daveroma)\
**Post date:** [June 29, 2020, 5:32pm UTC](https://community.hubspot.com/t/js-file-not-triggering-delay-after-submitting-form/21986/6 "2020-06-29T17:32:02Z")

</div>

Hey [@Alejandro\_](https://community.hubspot.com/t5/user/viewprofilepage/user-id/115716)

Glad you were able to use the code I gave you!

If my answer helped you solve your problem please mark it as the accepted answer and let me know if you have any other questions.

Thanks!
