In the quiz editor, open “Publish” → “On website” and set up how it’s shown
Choose where the quiz appears (a window over the site, a side panel or a block in the page) and how it opens, for example with the LeadForms button, auto-open or your website’s own links and buttons. Then click the “Publish” button at the bottom of the “Publish” tab: without it, your settings don’t reach your website.
Copy the LeadForms code
Open the “Site code” block and copy the code. It’s one code for the whole app, and it loads all your quizzes and widgets: you don’t need to add it to every component.
Paste the script into
index.htmlWhere to find the file: in Vite, it’s
index.html in the project root; in Create React App, it’s public/index.html. Paste the copied code right before the closing </body> tag.Why here and not in a component: React doesn’t run
<script> tags that end up in JSX, so code pasted there simply won’t run.<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My website</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
<script async src="https://script.leadforms.org/v1.js" data-key="YOUR_KEY"></script>
</body>
</html>
If you use Next.js
Next.js has no
index.html file. Load the script with the next/script component in the root layout, so it reaches every page:// app/layout.jsx
import Script from 'next/script'
export default function RootLayout(props) {
return (
<html lang="en">
<body>
{props.children}
<Script
src="https://script.leadforms.org/v1.js"
data-key="YOUR_KEY"
strategy="afterInteractive"
/>
</body>
</html>
)
}
Replace
YOUR_KEY with the key from the code you copied. Nothing else to call: quizzes and widgets load on their own.Open the quiz from your own button
The simplest way is to add the
data-leadforms attribute with the quiz ID to your button. No click handler needed:function QuizButton() {
return (
<button type="button" data-leadforms="8233">
Take the quiz
</button>
)
}
If you need to open the quiz from your own handler, call
Leadforms.open with the quiz ID:function openQuiz() {
if (window.Leadforms) {
window.Leadforms.open(8233)
}
}
Replace
8233 with your quiz ID: it’s the number in the direct link https://quiz.leadforms.org/ID.Using a link
The quiz also opens from a regular link with the address
#leadforms-ID:<a href="#leadforms-8233">Take the quiz</a>
Important: it has to be a plain
<a>, not <Link> from React Router or Next.js. The router would intercept the click and look for a route instead of passing the hash to the widget.If you want the quiz as a block inside the page
In “Publish”, choose “Block in the page” and add a container with the quiz ID to the component markup:
function QuizBlock() {
return <div data-leadforms-block="8233" />
}
The block is picked up on its own, even if the component mounts after the page has loaded. If the block still doesn’t show up, call
window.Leadforms.refresh() after the component mounts.What to keep in mind in an SPA
• The LeadForms code loads once, when the app opens. It doesn’t reload when you switch routes, and that’s expected.
• Blocks and buttons that appear when you open a route are picked up on their own. If something doesn’t show up, call
• A button with
• Blocks and buttons that appear when you open a route are picked up on their own. If something doesn’t show up, call
window.Leadforms.refresh().• A button with
data-leadforms and a Leadforms.open call work on any route with no extra steps.Stuck with the setup?
If something does not work, and we will sort it out with you.