Hello guys, I have a script inside my NextJS 14 app, that render a form from my CRM (Bitrix24).
bitrix-establishment-form.js
;(function (w, d, u) {
const s = d.createElement('script')
s.async = true
s.src = u + '?' + ((Date.now() / 180000) | 0)
const h = d.getElementsByTagName('script')[0]
h.parentNode.insertBefore(s, h)
})(window, document, 'https://cdn.bitrix24.com.br/{{SCRIPT_ID}}/crm/form/loader_14.js')
The only way I found to work is this:
EstablishmentForm.tsx
'use client'
import { useEffect } from 'react'
export const EstablishmentForm = () => {
useEffect(() => {
const scriptId = '/scripts/bitrix-establishment-form.js'
if (document.getElementById(scriptId) != null) {
return
}
const script = document.createElement('script')
script.id = scriptId
script.src = scriptId
script.setAttributeNS(null, 'data-b24-form', 'inline/{{ID}}/{{ID}}')
script.setAttributeNS(null, 'data-skip-moving', 'true')
script.defer = true
const scriptDiv = document.getElementById('script_div')
scriptDiv?.appendChild(script)
return () => {
scriptDiv?.removeChild(script)
}
}, [])
return <div id='script_div' className='w-full' />
}
I put the EstablishmentForm component inside a div. But there is a problem, the form only renders a few seconds after the page finishes loading.
I tried to use Next Script:
<Script
src='/scripts/bitrix-establishment-form.js'
data-b24-form='inline/14/vle4l0'
data-skip-moving='true'
/>
But another problem arises, the form is rendered in the end of the body, after everything in the page, but I read that this is the expected behavior (I think).
I tried to use the strategy='beforeInteractive'. The <script> is loaded in the <head> of the app, but the form is not rendered on the screen.
Could you help me to make this work, the form render when the page load?