Skip to main content

Close a Popup With JS Code

It is possible to close a popup from your code without user interaction through Wisepops' JS Callbacks feature.

Lisa Fockens avatar
Written by Lisa Fockens
Updated yesterday

Let's say we want our popup to close five minutes after being displayed if it has not been closed earlier by the visitor.

Create a Before-popup-display callback

Go to the JS section in the left-hand menu of the Campaign Builder, and paste the following callback that is triggered on the Before popup display event:

// Automatically close the popup after a delay
var delay = 5 * 60 * 1000; // 5 mins
setTimeout(function () {
event.detail.popup.close();
}, delay);

The event.detail.popup object is available only when listening to the Before popup display event. It exposes various methods, including one to close the popup.

Here we used the Wisepops interface to add the JS callbacks. You might want to write this code on your own website instead (to make it work on multiple popups or to inject back-end variables). Please refer to this article to learn how to do so.

Not seeing your popup on your website? Try these tips.

Close popup without closing popup tab

If you have popup tab that's displayed in the same pageview after popup is closed:

  • After a visitor closes the popup, or

  • After a visitor converts

the previous method won't work.

The previous snippet closes the campaign completely, including preventing display of a tab.

In this case, use the following JS snippet to click on close button of a popup after a certain delay, it'll close the popup without closing the tab:

const delay = 5 * 1000; // 5 second delay  

setTimeout( function () {
const closeButton = event.target.querySelector('.wisepops-close');
if(!closeButton){ return; }

closeButton.click();
}, delay);


Did this answer your question?