ADS-001
A popunder that actually opens behind the page
The swap technique, plus the one line that stops the visitor's own click from cancelling it — the reason most popunder code silently loses the traffic it was supposed to send.
A popunder has to leave the visitor looking at the page they were already reading. The naive version opens a new window and the browser puts it in front, which is a popup, which is what people install blockers for.
The swap technique inverts it. You open a new window pointed at the page the visitor is already on, so the browser raises that one and the visitor sees no change at all. Then you send the original window — now behind the new one — to the destination. What ends up in the background is the ad.
There is a failure that costs you almost all your traffic and looks like nothing is wrong. Popunders normally fire on a click, and that click is usually on a link or a button. The click does its own navigation, which overrides the redirect you just set on that window, so the destination never loads. The window swap worked; the traffic vanished. Cancelling the click's default action is what fixes it, at the price of that one click not doing what the user asked.
(function () {
var DEST = class="tk-s">&class="tk-c">#class="tk-n">039;https://example.com/landingclass="tk-n">039;; // where the background window goes
var CAP_HOURS = class="tk-n">6; class="tk-c">// one fire per visitor per this many hours
var fired = false;
function capped() {
try {
var until = parseInt(localStorage.getItem(class="tk-s">&class="tk-c">#class="tk-n">039;pu_untilclass="tk-n">039;) || class="tk-s">class="tk-n">039;class="tk-n">0class="tk-n">039;, class="tk-n">10);
if (Date.now() < until) { return true; }
localStorage.setItem(class="tk-s">&class="tk-c">#class="tk-n">039;pu_untilclass="tk-n">039;, String(Date.now() + CAP_HOURS * class="tk-n">3600 * class="tk-n">1000));
} catch (e) { /* private mode: fall through and fire anyway */ }
return false;
}
document.addEventListener(class="tk-s">&class="tk-c">#class="tk-n">039;clickclass="tk-n">039;, function (ev) {
if (fired || capped()) { return; }
fired = true;
class="tk-c">// The clickclass="tk-s">class="tk-n">039;s own navigation would override our redirect below.
ev.preventDefault();
class="tk-c">// Open the page the visitor is ALREADY on. The browser raises this window,
class="tk-c">// so from the visitorclass="tk-n">039;s point of view nothing happened.
var front = window.open(window.location.href, class="tk-s">&class="tk-c">#class="tk-n">039;_blankclass="tk-n">039;);
if (!front) { fired = false; return; } class="tk-c">// blocked: leave the click alone next time
front.focus();
class="tk-c">// This window is now behind. Send it to the destination.
window.location.href = DEST;
}, true);
}());
Using it
Serve this from your own domain as a script call rather than pasting it into every publisher's page. When the technique needs updating — and it will, browsers keep moving — everyone gets the new version without touching their site.
Cap the frequency per visitor. A popunder on every click is the fastest way to lose a publisher.
What bites people
Never repurpose the visitor's own window for the destination without cancelling the click. That is the bug: the swap looks perfect in testing, where you click on empty space, and fails on every real click, which is on a link.
Popup blockers only allow window.open inside a real user gesture. Calling it on a timer or after an async response will be blocked.
Losing one click per session is the cost of this technique. Fire on a click somewhere harmless if you can choose.