Skip to content

Instantly share code, notes, and snippets.

@dvygolov
Created March 9, 2024 20:10
Show Gist options
  • Save dvygolov/3cb59e22c720081fef55cab31dc8cb1e to your computer and use it in GitHub Desktop.
Save dvygolov/3cb59e22c720081fef55cab31dc8cb1e to your computer and use it in GitHub Desktop.
simplest onclick popunder possible
class Popunder {
constructor(url) {
this.targetUrl = url || "http://google.com";
this.cookieName = "popunder";
this.clicksRequired = 1;
this.expirationHours = 24;
this.cookiePath = ";path=/";
this.eventName = "click";
this.documentRoot = document.documentElement;
this.init();
}
openPopunder() {
if (!document.cookie.match(new RegExp(`(^|\\W)${this.cookieName}=1(\\W|$)`))) {
window.open(this.targetUrl, this.cookieName, "width=1024,height=768,resizable=1,toolbar=1,location=1,menubar=1,status=1,scrollbars=1");
window.focus();
let expiresDate = new Date();
expiresDate.setTime(expiresDate.getTime() + 3600 * this.expirationHours * 1000); // Convert hours to milliseconds
document.cookie = `${this.cookieName}=1; expires=${expiresDate.toGMTString()}${this.cookiePath}`;
}
}
eventHandler = () => {
if (--this.clicksRequired === 0) {
this.openPopunder();
}
}
init() {
if (typeof this.documentRoot.addEventListener !== "undefined") {
this.documentRoot.addEventListener(this.eventName, this.eventHandler, false);
} else if (typeof this.documentRoot.attachEvent !== "undefined") {
this.documentRoot.attachEvent("on" + this.eventName, this.eventHandler);
}
}
}
// Usage example
const myPopunder = new Popunder("http://example.com");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment