---
title: Randomly Display Popups
description: "## Programmers Guide Given a list of popups, you can randomly display one for a given browser session. This guide gives you custom code to let you do that. The JavaScript code below saves a [cookie](https://wppopupmaker.com/docs/getting-started/popup-maker-glossary-of-terms/#cookies) so that only the…"
url: "https://wppopupmaker.com/docs/manage-popup-display/a-b-test-or-randomly-display-popups/"
updated: "2026-10-19"
category: How To / Manage Popup Display
---

# Randomly Display Popups

## Programmers Guide

Given a list of popups, you can randomly display one for a given browser session. This guide gives you custom code to let you do that.

The JavaScript code below saves a [cookie](https://wppopupmaker.com/docs/getting-started/popup-maker-glossary-of-terms/#cookies) so that only the chosen option can be viewed by the same visitor for a browser session. That means you can "manually" run basic split tests with Popup Maker popups.

### Want to split test popups without writing code?

Check out how [Popup Maker's new built-in split testing](https://wppopupmaker.com/guides/how-to-split-test-a-popup-in-popup-maker/) feature lets you see which variations of your popup do better. You can split test your popup's headlines, content blocks, settings, and even compare different popups.

  [Learn more](https://wppopupmaker.com/guides/how-to-split-test-a-popup-in-popup-maker/)

Make sure you track your popup open stats with the [Popup Analytics](https://wppopupmaker.com/extensions/popup-analytics/) or another analytics tool.

If you're new to using JavaScript with Popup Maker, check out our [Getting Started with Custom JavaScript Doc](http://docs.wppopupmaker.com/article/84-getting-started-with-custom-js).

### Code Snippet

**Remember to customize the code variables as shown in the code sample below.**

```
jQuery(document).ready(function($) {    // Define the configuration variables for the A/B split test.    const popups = [388, 407], // Comma-separated popup IDs. Change to your popup IDs.        cookieName = 'pum-atc-split-test', // Cookie name for this specific test. Change the name to what you want.        cookieTime = '1 month'; // Duration for the cookie to be stored. You can change this too.    // This variable will hold the ID of the chosen popup. It is initialized to false.    let chosenPopup = false;    /**     * Selects and returns a random popup ID from the `popups` array.     *      * This runs when there's no split test cookie set yet.     */    function randomPopup() {        return popups[Math.floor(Math.random() * popups.length)];    } // Closes randomPopup()    /**     * Retrieves the chosen popup ID.     *      * - It first checks if a cookie with the chosen popup already exists.     * - If a valid cookie is found, it returns that popup ID.     * - Otherwise, it calls randomPopup() to get a new one and sets the cookie.     */    function getChosenPopup() {        let popup;        let cookie;        // Return 0 if the cookie function is not available.        if ($.pm_cookie === undefined) {            return 0;        } // Closes if ($.pm_cookie === undefined)        // Attempt to retrieve and parse the cookie value.        cookie = parseInt($.pm_cookie(cookieName)) || false;        // If the cookie contains a valid popup ID from our array, use it.        if (cookie > 0 && popups.indexOf(cookie) !== -1) {            popup = cookie;        // If no cookie exists, choose a random popup and set the cookie.        } else if (!cookie) {            popup = randomPopup();            $.pm_cookie(cookieName, popup, cookieTime, '/');        } // Closes if/else if        return popup;    } // Closes getChosenPopup()    /**     * Event listener that fires before any popup opens.     *      * It checks if the popup is part of our split test and prevents it     * from opening if it was not the one chosen for the session.     */    $(document).on('pumInit', '.pum', function() {        const $this = $(this);        const popupId = $this.popmake('getSettings').id;        // If the chosen popup hasn't been determined yet, get it.        if (!chosenPopup) {            chosenPopup = getChosenPopup();        } // Closes if (!chosenPopup)        // Check if the opening popup is in our test group but is NOT the chosen one.        if (popups.indexOf(popupId) >= 0 && popupId !== chosenPopup) {            // If it's not the chosen one, add a class to prevent it from opening.            $this.addClass('preventOpen');        } else {            // Otherwise, ensure the 'preventOpen' class is not present.            $this.removeClass('preventOpen');        } // Closes if/else    }); // Closes $(document).on()}); // Closes jQuery(document).ready()
```
