Which works perfectly, however when I add an eventlistener like this
<script>
window.addEventListener('hs-form-event:on-ready', (event) => {
const { formId } = event.detail;
const timestamp = new Date().toISOString();
console.log(
`💡 Form ready event fired for Form ID: ${formId} at ${timestamp}`
);
});
</script>
I can see that in my Console tab, that it fired twice.
What could be causing this?
I want to inject some data dynamically into a hidden field and I’m not sure if this is a realiable way of checking that my form is loaded and ready.
Hey @NRak have you tried looking for the formInstance instead of the formID? My guess is maybe there’s some multiple loads of your form happening due to the webflow implementation.
This still happens when I test the embed code alone in a sandbox. It looks like this may be caused by the server side rendering of the form, there’s two instances of the form ID loading, one is triggered by the load in your DOM and one is triggered by the server side rendering. My guess is this may be intentioanal but I can’t be sure.
@CommunityTeam would it be possible to check with the forms team to check if this is intentional?
// This "flag" tracks if our code has run
let formReadyHasRun = false;
window.addEventListener('hs-form-event:on-ready', (event) => {
// 1. Check the flag. If it's true, stop here.
if (formReadyHasRun === true) {
return;
}
// 2. If it's false, run code...
console.log("Form is ready, running my code!");
// ... (Your code to inject data goes here) ...
// 3. ...then, flip the flag to true.
formReadyHasRun = true;
});
(Note - I used AI to format my code example) — Jaycee