简体   繁体   中英

Javascript PopUp once per day

I have javascript popup it shows up per user (only once per day) and i need same popup but second.. when i try just to create second popup with same code it works like first one. if user saw first popup, he/she can't see other. please help here is the code

 if (document.cookie.indexOf('_visited=1') == -1) { var delay_popup = 1000; setTimeout("document.getElementById('parent_popup').style.display='block'", delay_popup); var date = new Date; date.setDate( date.getDate() + 1 ); // Current date + 1 day document.cookie = '_visited=1; path=/; expires=' + date.toUTCString(); } 

I don't want them to work like 1 Popup

I just had this problem today.

what i did is this

enter code here


function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }


}


var myCookie = readCookie('daycookie');

if (myCookie=''){
   // code to show modal here 
   // then set an expiring token at midnight 
   var date = new Date();
   var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59);
   var x = Math.floor((Math.random() * 1000) + 1);
   document.cookie = 'daycookie' + "=" + x + "; " + "expires="+midnight;
}

the logic is at first if the cookie does not exist show the popup once the pop up shows it creates a cookie the expires at midnight and until next the day will the pop up show again.

i hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM