PopupBuilder.io

Exit-intent in JavaScript: desktop only, and not a consent trick

Emel DalabasmazUpdated August 23, 20264 min read

Exit-intent in JavaScript means watching the pointer leave the viewport toward the browser chrome. That event does not exist on phones. beforeunload is not a substitute: browsers ignore custom messages, and using it as a sales wall is hostile. Build a desktop detector if you must, add a mobile stand-in, and never run it before cookie and marketing consent.

Child stacking blocks

What is exit-intent, technically?

On a mouse-driven desktop, people often move toward the tab close or the address bar. A mouseleave on document.documentElement with a low clientY is the usual signal. That is a hint, not a promise they will leave.

It is useful for a last email or cart offer after they have seen the page. Google's intrusive interstitial rule is about covering content on arrival, especially from search on mobile. An exit overlay on desktop after a minute of browsing is a different object. Do not use the same full-screen wall on a phone at second one.

Omnisend still saw delayed forms beat immediate ones (2.4% vs 1.9% in 2025, 1.24 billion displays). Exit-intent is another form of "late." It is not magic.

JavaScript on a screen

How do you detect it on desktop without using beforeunload?

Do not use beforeunload for a marketing popup. The MDN beforeunload page is clear: modern browsers ignore custom strings, and the event is for unsaved-data warnings. This is the old snippet you should not ship for offers:

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "Are you sure you want to leave?";
  e.returnValue = confirmationMessage;
  return confirmationMessage;
});

That is a browser confirm, not an overlay, and Chrome will not show your copy.

A minimal desktop detector:

(function () {
  var shown = false;
  var minMs = 8000;

  function show() {
    if (shown) return;
    if (Date.now() - start < minMs) return;
    if (window.matchMedia("(pointer: coarse)").matches) return;
    shown = true;
    document.getElementById("exit-offer").hidden = false;
  }

  var start = Date.now();
  document.documentElement.addEventListener("mouseleave", function (event) {
    if (event.clientY > 0) return;
    show();
  });
})();

Notes:

  • (pointer: coarse) skips phones and many tablets.
  • shown is a session flag. Persist it in sessionStorage if you must, after you have a legal basis to store it. A consent-mode cookie is PECR territory in the UK (ICO).
  • Wait a few seconds so a misaimed cursor on load does not fire.
  • Put a real close button on #exit-offer. Trap focus while it is open, restore it on close.

The named-function version is the same logic:

function handleMouseleave(event) {
  if (event.clientY > 0) return;
  // open the overlay once
}

document.documentElement.addEventListener("mouseleave", handleMouseleave);

Style the overlay in your CSS. Keep it partial, not a 100% takeover, if the page is a search landing.

What do you do on mobile, where there is no mouse?

There is no honest "exit-intent" on iOS Safari. Common stand-ins, each with costs:

  • Inactivity on cart or a long article (20 to 30 seconds with no scroll). Easy to fire on a forgotten tab; pair with document.visibilityState === "visible".
  • Scroll-up after passing 50% depth. Sometimes means "heading to the header," often means reading.
  • History popstate. Easy to break the back button. Do not hijack Back for a coupon.
  • A tab or button the person opens. This is the cleanest.

Sleeknote 2026 had mobile converting at 5.60% vs 2.86% desktop across 516 million views. That is mostly late sheets and offer forms, not mouse tracking. Build the mobile campaign as a sheet or bar.

Vendor tools already wrap this. OptiMonk still has unreliable mobile exit-intent in our profile. Poptin is a reasonable small-site pick if you want the detector without writing it. OptinMonster is the pick if you will test desktop exit against a mobile inactivity variant.

The detector itself may set a "already shown" cookie. In the EU/UK that is usually non-essential if it is for marketing frequency, so it belongs after a CMP yes. The overlay that then asks for an email is a second processing: GDPR Article 7, PECR for electronic mail. An SMS field is TCPA written consent in the US.

Do not fire the overlay on top of the cookie banner. Do not treat a close click as email consent.

If you would rather not maintain this, use a builder and still set a delay, a frequency cap, and a mobile-specific campaign. Popupsmart will get a desktop exit campaign live quickly. It will not invent a mouse on a phone.

Hand holding a compass

About the author

Emel Dalabasmaz
Emel Dalabasmaz

SEO Executive at Flatart

Emel Dalabasmaz graduated in Advertising and Public Relations from Anadolu University. She began in social media and carried that grounding — content, brand communication and performance — into SEO. For more than ten years she has worked on growth for brands across a range of industries, pairing a communications perspective with data and technical analysis to build a digital presence that holds up over time. At Flatart, she has worked for more than ten years with over 250 brands and set up popups for more than 70% of them. She has tried the popup tools covered by PopupBuilder.io for agency customers and read G2 customer reviews as part of her product research.

Education
Advertising and Public Relations, Anadolu University
Writes about
Popup builders, Conversion rate optimization, On-site messaging, SaaS pricing research, SEO, Technical SEO, Content strategy, Digital marketing