How to open two popup one at a time when updating the tab?

Asked

Viewed 137 times

0

I intend to use two popup ads on my site, as I do to open one at a time when refreshing the page I tried, but it opens two pop-ups at the same time irritating any visitor, I tried the following script.

(function () {
var isOpened = false;

var siteUrl  = "http://popup1";
var siteUrl  = "http://popup2";

document.addEventListener("click", function(){
    if (!isOpened) {
        isOpened = !!window.open(siteUrl, "_blank");
    }
  });
})();
  • @Márcioeric, just make comments that try to solve the problem.

1 answer

0

Sorry for the comment, but using setInterval, an auxiliary variable and a method is simple. Follow an example:

var openedAds = 0;
var AdsToOpen = ['http://popup1','http://popup1'];
var interval;

var interval = setInterval(function(){
    window.open(AdsToOpen[openedAds], "_blank");
    openedAds += 1;
    if(openedAds === AdsToOpen.length){
        clearInterval(interval);
    }
}, 2000); 

Adstoopen are the website you intend to open.
2000 at the end of the code is the time between a popup and another (in ms)

  • This method that you gave me opens both the same way, I want something that opens the first popup only once, then when refresh the tab open the second popup all individually.

  • In this way, you could use cookies, but why not assortment the ad? like, you have a 50% chance to open one ad or another, so it’s assorted the ad that will open.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.