Sub topic 8
When a pop-up should appear
This chapter assumes you can write basic HTML and JavaScript and know what an email service provider does. It covers how to choose a trigger for a pop-up, how to build one with the native dialog element, and the errors that make pop-ups fail.
What a trigger does
A trigger is the event that opens the pop-up. The trigger decides whether the visitor sees your offer while they are interested or while they are busy with something else. A pop-up that opens on page load asks for an email address before the visitor knows what the site offers. Most visitors close it without reading.
The common triggers are these:
| Trigger | Fires when | Best for | Main risk |
|---|---|---|---|
| Time delay | N seconds after load | Short pages | Interrupts reading |
| Scroll depth | Visitor passes a set share of the page | Articles and guides | Never fires on short pages |
| Exit intent | Mouse leaves toward the browser chrome | Desktop visitors who are leaving | Does not exist on touch screens |
| Click | Visitor selects a link or button | Lead magnets | Needs a visible call to action |
Scroll depth is a good default for content pages. A visitor who has read half an article has shown interest, and the pop-up can name the offer that fits that article.
Build the pop-up
Use the native dialog element. Opening it with showModal() moves focus into the dialog, closes it on Escape, and dims the page behind it. You do not need a library for these behaviors.
<dialog id="signup-dialog" aria-labelledby="signup-title">
<h2 id="signup-title">Get the free checklist</h2>
<form action="https://example.com/subscribe" method="post">
<label for="popup-email">Email address</label>
<input type="email" id="popup-email" name="email" autocomplete="email" required>
<button type="submit">Send it to me</button>
</form>
<form method="dialog">
<button>No thanks</button>
</form>
</dialog>The second form uses method="dialog". Submitting it closes the dialog without any script.
Add a trigger and a frequency cap
The script below opens the dialog when the visitor has scrolled halfway down the page. It also records when the dialog closes, and it stays hidden for seven days after that.
const KEY = 'signupDismissedAt';
const COOLDOWN_MS = 7 * 24 * 60 * 60 * 1000;
const dialog = document.querySelector('#signup-dialog');
function recentlyDismissed() {
const t = Number(localStorage.getItem(KEY));
return t > 0 && Date.now() - t < COOLDOWN_MS;
}
function onScroll() {
const scrollable = document.documentElement.scrollHeight - window.innerHeight;
if (scrollable > 0 && window.scrollY / scrollable >= 0.5) {
window.removeEventListener('scroll', onScroll);
dialog.showModal();
}
}
if (!recentlyDismissed()) {
window.addEventListener('scroll', onScroll, { passive: true });
}
dialog.addEventListener('close', () => {
localStorage.setItem(KEY, String(Date.now()));
});Three details in this code matter:
- The
scrollable > 0check avoids dividing by zero on pages that do not scroll. removeEventListenerruns beforeshowModal(). CallingshowModal()on a dialog that is already open throws an error, so the handler must run only once.- The
closeevent fires for both "No thanks" and Escape, so both count as a dismissal. A subscriber who submits the form also triggers it, which is acceptable here because that visitor does not need the pop-up again.
The flow of a visit
Common errors
- Triggering on load or after a few seconds. The visitor has not seen your content yet, so they have no reason to subscribe.
- No frequency cap. Without one, a returning visitor sees the pop-up on every page.
- Exit intent as the only trigger. Touch devices have no mouse pointer, so mobile visitors never see the pop-up.
- Listening to scroll without
passive: true. This can slow scrolling on low-powered phones. - Storing the dismissal only in memory. The cap is lost on the next page load.
Exercise
A visitor reads a short page that fits entirely in the browser window. The scroll trigger above never opens the dialog. Which line of the code causes this, and what trigger would you add as an alternative?
Show answer
The check scrollable > 0 fails because the page has no scrollable height, so onScroll never opens the dialog. Add a time delay trigger (for example, 30 seconds after load) or a click trigger on a call-to-action link that calls dialog.showModal().
Exercise
Why does the script call removeEventListener before dialog.showModal() instead of after?
Show answer
The scroll event fires many times. Removing the listener first guarantees showModal() runs only once. Calling it on an already open dialog throws an error.