Sub topic 8
When a pop-up should appear
A pop-up that appears at the wrong moment interrupts a visitor who has not yet seen any reason to subscribe. The same pop-up at the right moment reads as a useful offer. This chapter covers the main triggers, how to implement them correctly, and the mistakes that cause trouble.
It assumes you can write basic HTML and JavaScript, know what an email service provider (ESP) does, and have read the earlier chapters on offers, placement types, and building the sign-up form.
What a trigger is
A trigger is the condition that makes a pop-up open. Each trigger is a guess about when a visitor is interested enough to consider subscribing. A good trigger depends on evidence of engagement, such as reading, scrolling, or leaving. A bad one depends only on arrival.
The common triggers are:
| Trigger | Fires when | Strength | Main risk |
|---|---|---|---|
| Time delay | N seconds after page load | Simple to build | Interrupts people who have not read anything |
| Scroll depth | The visitor passes a percentage of the page | Shows real reading | Never fires on short pages |
| Exit intent | The pointer moves toward leaving the page | Appears when the visitor is already leaving | Works only on desktop with a mouse |
| Click | The visitor clicks a link or button | The visitor asked for it | Needs a visible call to action |
| Return visit | The visitor has been here before | Targets repeat readers | Needs stored state |
The click trigger has the highest intent, because the visitor chose to open the form. Use it whenever you can offer a button such as "Get the checklist".
Scroll depth
Scroll depth measures how far down a page the visitor has gone, as a fraction of the page that can be scrolled. A pop-up at 50% means the visitor has probably read part of the article.
This is the trigger used in the example below, together with a frequency cap. It is built on the native dialog element from the earlier chapters, so it keeps focus handling and Escape-to-close.
<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>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()));
});Several details in this code matter:
scrollableis the page height minus the viewport height. Dividing by it gives a fraction between 0 and 1. Dividing by the whole page height would never reach 1.- The
scrollable > 0check avoids dividing by zero on pages that fit on one screen. On such pages the pop-up never opens through scrolling. Pair scroll with another trigger if short pages matter to you. removeEventListenermakes the pop-up fire once per page view instead of on every scroll event after 50%.{ passive: true }tells the browser the handler will not block scrolling, which keeps scrolling smooth.- The
closeevent fires for every way of closing the dialog: the "No thanks" button, Escape, or a successful form flow that closes it. Storing the time there gives one cap for all of them.
Scroll events fire constantly
The handler above runs on every scroll event. Its work is small, but on heavy pages you can use an IntersectionObserver on a marker element placed halfway down the article. It runs only when the marker enters the viewport and does no per-scroll arithmetic.
const marker = document.querySelector('#scroll-marker');
const observer = new IntersectionObserver((entries) => {
if (entries.some((e) => e.isIntersecting)) {
observer.disconnect();
document.querySelector('#signup-dialog').showModal();
}
});
observer.observe(marker);This version assumes a <div id="scroll-marker"></div> exists in the page. Without that element, observe(null) throws an error.
Time delay
A time delay opens the pop-up after a fixed number of seconds. It is the easiest trigger to build and the easiest to misuse.
const delayMs = 30000;
setTimeout(() => {
const dialog = document.querySelector('#signup-dialog');
if (!dialog.open) dialog.showModal();
}, delayMs);A delay measures time on the page, not attention. A visitor may have the tab open in the background. A delay of a few seconds shows the offer before the visitor knows what the site offers. Delays of 30 seconds or more, or delays combined with scroll, perform better in most reports, but your own test results matter more than general advice. A later chapter covers how to run that test.
Two edge cases:
- The timer keeps running in a background tab, so the pop-up may be waiting when the visitor returns. You can check
document.visibilityState === 'visible'before opening it. - Calling
showModal()on a dialog that is already open throws anInvalidStateError. Checkdialog.openfirst, as the example does.
Exit intent
Exit intent detects a pointer moving toward the top of the browser window, where the tabs and address bar are. It is a last-chance offer, and it does not block reading, because the visitor was leaving anyway.
document.addEventListener('mouseout', (event) => {
const leavingTop = event.clientY <= 0 && event.relatedTarget === null;
const dialog = document.querySelector('#signup-dialog');
if (leavingTop && !dialog.open) dialog.showModal();
});The relatedTarget === null check means the pointer left the document and did not just move onto another element.
Know its limits:
- Touch screens have no pointer, so there is no signal. Mobile versions use proxies, such as a fast scroll upward or pressing the back button. Those are unreliable, and mobile interstitial rules apply, which a later chapter covers.
- Visitors who close a tab with the keyboard give no signal.
- If the visitor is already inside a form, a pop-up on top of it is a bad experience. Check that no field has focus before opening it.
Combining triggers
Single triggers often misfire, so combine them into a rule. For example, "open after the visitor has scrolled 50% and spent at least 20 seconds on the page" filters out fast scrollers. A function that checks each condition keeps this readable:
const state = { scrolled: false, waited: false };
function maybeOpen() {
const dialog = document.querySelector('#signup-dialog');
if (state.scrolled && state.waited && !dialog.open) {
dialog.showModal();
}
}
setTimeout(() => { state.waited = true; maybeOpen(); }, 20000);
window.addEventListener('scroll', () => {
const scrollable = document.documentElement.scrollHeight - window.innerHeight;
if (scrollable > 0 && window.scrollY / scrollable >= 0.5) {
state.scrolled = true;
maybeOpen();
}
}, { passive: true });The decision flow
Before any trigger opens a pop-up, run the same checks. The order puts the cheapest and most respectful checks first.
Add a "subscribed" flag to storage when the form succeeds. Showing a sign-up prompt to someone who already subscribed is the most common complaint about pop-ups.
Common errors
- Firing on page load. The visitor has seen nothing yet, and search engines may treat an immediate full-screen overlay as an intrusive interstitial.
- Forgetting storage failures.
localStoragecan throw an error in private browsing modes or when storage is blocked. Wrap reads and writes intry/catch, and fall back to showing the pop-up less often. - Ignoring consent rules. Storing a dismissal time on the visitor's device is generally treated as functional, but check the rules that apply to your audience before adding tracking to your pop-up logic. Consent is the topic of a later chapter.
- Stacking triggers. Two separate scripts, such as a scroll trigger and an exit trigger, can both call
showModal(). The second call throws. Share oneopenfunction that checksdialog.open. - Hiding the close control. Every pop-up needs an obvious way out that works with keyboard and touch. The native
dialoghandles Escape. Keep the "No thanks" button visible. - Counting a view as a dismissal. If you store the time when the pop-up opens, a visitor who subscribes is also capped. That is usually harmless, but a separate "subscribed" flag is clearer.
Choosing a trigger
Match the trigger to the page:
- Long articles: scroll depth, optionally with a minimum time.
- Short landing pages: a click trigger or exit intent on desktop.
- Content upgrades: a click on a button that offers the related download.
- Returning readers: a return-visit rule, with a longer cooldown after a dismissal.
Start with one trigger, record how many people see the pop-up and how many subscribe, and change one thing at a time.
Exercise
A pop-up uses window.scrollY / document.documentElement.scrollHeight >= 0.5. On a long article it opens much later than the halfway point. Why does this happen, and what is the fix?
Show answer
The visitor can never scroll the full page height, because the viewport covers part of it. When the page is scrolled to the bottom, scrollY equals the page height minus the viewport height, so the ratio stays below 1 and the 50% mark is reached late. The fix is to divide by the scrollable distance: scrollY / (scrollHeight - innerHeight), and to check that this distance is greater than 0.
Exercise
You add both a scroll trigger and an exit-intent trigger, each calling dialog.showModal() directly. Sometimes the console shows an InvalidStateError. What causes it, and how do you prevent it?
Show answer
Calling showModal() on a dialog that is already open throws InvalidStateError. When both triggers fire for the same visitor, the second call fails. Route both triggers through one function that returns early when dialog.open is true, and also check the cooldown and subscribed flag there.
Exercise
Name two reasons a time-delay trigger can show a pop-up to a visitor who is not engaged, and one rule that reduces the problem.
Show answer
The timer counts time on the page, not attention, so a background tab or a visitor who has not scrolled still triggers it. A short delay also opens the pop-up before the visitor has seen the content. Combine the delay with scroll depth, such as at least 20 seconds and 50% scrolled, and check document.visibilityState.