Hi everyone.
My Goal: “I am trying to use the hbspt.forms.create JavaScript function to embed a form so I can use the onFormReady callback to populate a hidden field.”
<div id="hubspot-form-target-final"></div>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
hbspt.forms.create({
region: "na2",
portalId: "PORTALID",
formId: "FORMID",
target: '#hubspot-form-target-final',
onFormReady: function($form) {
console.log('Success! onFormReady callback has fired.');
const formElement = $form[0];
const hiddenField = formElement.querySelector('input[name="0-1/giveaway_url"]');
if (hiddenField) {
const currentUrl = new URL(window.location.href);
const cleanUrl = currentUrl.origin + currentUrl.pathname;
hiddenField.value = cleanUrl;
const inputEvent = new Event('input', { bubbles: true });
hiddenField.dispatchEvent(inputEvent);
console.log('Hidden field populated successfully with:', cleanUrl);
} else {
console.error('Field not found. Please inspect the form to get the correct "name" attribute.');
}
}
});
</script>
The Problem: “The form appears visually on my page, but the onFormReady callback never fires. The browser console shows a 403 Forbidden error for a resource being loaded from forms-na2.hsforms.net.”
What I’ve Already Done: "I have already confirmed the following:
The issue persists in a Chrome Guest/Incognito window, ruling out browser extensions.
All caching plugins (like WP Rocket) and server-side caches are disabled.
My domain is correctly added to the ‘Limit tracking to these domains’ list in my HubSpot settings.
The form does not have CAPTCHA or special GDPR consent settings enabled."
