A custom JavaScript event trigger opens your popup when your own page says so: you name an event in the popup editor, then fire that name from a button or a line of script. Nothing waits for a timer, a scroll point or an exit intent. The visitor acts, your code fires the event, the popup opens.

Key takeaways

  • Set the popup’s trigger to a custom event and type one exact name; the popup opens only when that name fires on a page the popup targets.
  • Fire the event from your own code with window.dispatchEvent(new CustomEvent('your-event-name')), inside a click handler or at the point where the moment you care about happens.
  • Keep the name identical everywhere, lowercase with hyphens, and copy it from the popup editor instead of retyping it.
  • Load the popup script once before the closing body tag, then test on the live page, because the preview will not catch a missing script or an error elsewhere in your JavaScript.

What a custom event trigger is, and when you need one instead of a timer

A custom event trigger is a popup trigger that waits for a named signal from your own page code rather than a built-in behavior like a timer, a scroll depth or an exit intent.

Reach for one when the moment that should open the popup already exists on the page. On a landing page it might be a “Which plan fits me?” button. In a shop it might be a size guide link or a delivery estimate. Your code already knows when those happen, so a custom event lets the popup use the same moment. If the page is a campaign page, the landing page popup guide covers which kind of popup suits it.

The other triggers are guesses. A timer guesses that a visitor is ready after some seconds. Exit intent bets they are leaving. Scroll depth assumes reading means interest. A custom event carries no guess: it fires when the action you chose happens, and only then.

Most popups do not need one. If the popup should appear after the visitor has read a little, the built-in scroll trigger is the right tool, and the popup scroll trigger guide explains how depth is measured. Use a custom event for moments the built-in triggers cannot see.

Step 1: Paste the popup script once, before the closing body tag

The popup can only listen for your event if its script is on the page, so the first job is one script tag before the closing body tag of every page where the event might fire. On a hand-built site that usually means the shared footer template. In a site builder it is the custom code or footer code field, the one that injects before </body>.

The popup itself is built in PopupGenius, for on-brand upsell and email popups on your website, and it goes live with that same single script tag; the free plan needs no card. If placing a script on this site is new to you, the walkthrough for adding a popup to any website with one script tag covers where the common builders keep that field.

Paste it once. A second copy on the same page is easy to miss in a code box and hard to explain when the popup behaves differently from the preview. Also check that the tag sits in the template the page actually uses: many builders let a single page take a blank layout that skips the footer include.

Step 2: Set the trigger to a custom event and give the event a name

In the popup editor, choose the custom event as the trigger and type the event’s name into the field it offers. That name is the whole contract between your page and the popup, and it has to match character for character, including case.

Pick a name that says what happened, not what the popup is called: size-guide-open, demo-form-sent, pricing-compare-clicked. Keep it lowercase, use hyphens rather than spaces, and give each popup its own name. Event names are case sensitive, so SizeGuideOpen and sizeguideopen are different events, and a trailing space pasted in from a document will quietly break the match.

In PopupGenius you set the trigger to the custom event and type the name once, and the popup opens whenever that name fires on a page it targets.

Two more settings decide what happens after the popup has opened. Frequency controls whether a visitor sees it every time, once per visit, or once every few days; for a button someone may press more than once, once per visit is usually the kinder setting. Page targeting is a set of URL rules, so the popup can listen on the pages you choose and ignore the event everywhere else.

Step 3: Fire the event from your own button or script

To open the popup from a button, attach a click listener and dispatch a custom event with the same name. This is the browser’s own CustomEvent API, not a library:

<button type="button" id="size-guide-button">Which size should I pick?</button>

<script>
  document.getElementById('size-guide-button').addEventListener('click', function () {
    window.dispatchEvent(new CustomEvent('size-guide-open'));
  });
</script>

Three details matter here. type="button" stops the button from submitting a form if you drop it inside one. The listener runs on the visitor’s click, which normally comes after the page and the popup script have loaded. And the string in the CustomEvent call must be the exact name you typed in Step 2.

If no button is involved, fire the same line wherever your code knows the moment has arrived: after a form submission succeeds, when a video ends, when the visitor switches a pricing toggle, or when a fetch returns what you were waiting for.

window.dispatchEvent(new CustomEvent('size-guide-open'));

Fire it once, at the moment itself. Calling it on every page load is the same as a timer set to zero, and it will open the popup before the visitor has done anything. The popup script loads after the rest of the page, so an event fired during load can also arrive before anything is listening.

Firing the same event from more than one place

One event name can be fired from as many places as you like, which is the useful part: three size guide links across the page can all open the same popup. Keep the name in one variable so a rename is a single edit rather than a search through the template.

const OPEN_SIZE_GUIDE = 'size-guide-open';
window.dispatchEvent(new CustomEvent(OPEN_SIZE_GUIDE));

Two popups need two names. Event names are how you keep the intent readable six months later, when size-guide-open still says what it does and popup2 does not.

Treat the name as the only handshake. It opens the popup; it does not tell the popup which button was clicked. If two audiences need different copy, build two popups with two names rather than trying to make one event carry the difference.

Test it before you publish

Test in two places, because each catches something the other misses. In the popup editor’s preview, check the design, the button copy and where the flow goes after a click. Then test on the live page, where your own script actually runs.

On the live page, open it in a private window so a cached version does not confuse you. Open the browser console, add a temporary listener for your event name, and click the button:

window.addEventListener('size-guide-open', function () {
  console.log('size-guide-open fired');
});

If the message appears but no popup does, the problem is on the popup’s side: the name, the targeting or the frequency. If no message appears, the problem is in your page code and the popup is not at fault. Repeat the check on the real domain, since preview URLs are a classic place for page targeting rules to stop matching.

PopupGenius keeps edits as a draft until you click Update, so a change you are testing does not reach the live popup until you decide it should.

Why the popup does not open: the usual causes

Most failures come down to one of two things: the event never fires, or it fires and the popup declines to open. The first is in your page code, the second is in the popup’s settings.

The event never fires

The script tag is missing on that page. A landing page with its own layout, or a page built from a different template, can skip the footer where you pasted it.

The click handler runs before the button exists. If the script sits above the button in the HTML, getElementById returns nothing and the listener is never attached. Move the script below the button, or listen on document and check the clicked element inside the handler.

An earlier JavaScript error stopped everything after it. Open the console and read the first red line, not the last one.

The element you attached to was replaced. Some page builders re-render a form or a widget after load, which throws the listener away. Attaching to a parent element that stays put avoids this.

The event fires but the popup stays shut

The name does not match. Compare the two strings side by side, including case and any trailing space, and remember that a name copied out of the editor is safer than one retyped.

The page targeting rules exclude this URL. Check them against the exact address you are testing, including whether it has a trailing slash.

The frequency setting is doing its job. If the popup is set to once per visit or once every few days, a second click in the same session will not reopen it. Use a private window, or switch the setting to every time while you test.

The trigger setting did not save, or an older version of the popup is still live. Confirm the trigger still reads custom event and that the published version matches the draft.

The event is dispatched on a different target than the one the popup listens to. If the console shows the event firing and everything else looks right, try dispatching on document instead of window, which is the other common place a listener sits.

FAQ

Yes. Attach the same click listener to the link and call event.preventDefault() inside it if you want the popup to hold the visitor on the page. Without that call the browser follows the link and the next page loads, so the popup may appear only for a moment.

Does the event work on every page of my site?

Only where two conditions are met: the popup script is loaded on that page, and the popup’s URL targeting includes it. Firing the event elsewhere does nothing, which is useful, because you can leave the dispatch call in a shared component without popups appearing in the wrong place.

Can one event open two different popups?

A page shows one popup at a time, so two popups listening for the same event would compete for the same moment. Give each popup its own event name and fire the one that matches the action the visitor took. It also keeps your code readable when you come back to it later.