Display popup only once per visit

扶醉桌前 提交于 2020-01-02 19:53:08

问题


I want to show popup just once per session which expire after some time. Can someone help me?

function PopUp(){
    $('.home-popup').fadeIn(500);
}

setTimeout(function(){
  PopUp();
},1000); // 1000 to load it after 1 second from page load

$('.close-popup-btn').click(function() {
    $('.popup').fadeOut(300);
});

回答1:


You could use localstorage for this as well. To set a storage item: localStorage.setItem('myPopup','true'); and to check for it you could do something like this:

var poppy = localStorage.getItem('myPopup');

if(!poppy){
    function PopUp(){
        $('.home-popup').fadeIn(500);
    }

    setTimeout(function(){
        PopUp();
    },1000); // 1000 to load it after 1 second from page load

    $('.close-popup-btn').click(function() {
        $('.popup').fadeOut(300);
    });
    localStorage.setItem('myPopup','true');
}



回答2:


I would set a cookie with the popupSent value on the first visit, and check if the cookie exists before calling the PopUp function. Here is a rough implementation with the cookie helper functions from here: Set cookie and get cookie with JavaScript

function PopUp(){
    $('.home-popup').fadeIn(500);
    createCookie('popup','1');
}
if(readCookie('popup')){
    // 1000 to load it after 1 second from page load
    setTimeout(PopUp,1000); 
}
$('.close-popup-btn').click(function() {
    $('.popup').fadeOut(300);
});



回答3:


if by "per session" you mean "per page load":

$(document).ready(function(){
 function PopUp(){
     $('.home-popup').fadeIn(500);
 }

 setTimeout(function(){
   PopUp();
 },1000); // 1000 to load it after 1 second from page load

 $('.close-popup-btn').click(function() {
    $('.popup').fadeOut(300);
 });
});


来源:https://stackoverflow.com/questions/41857227/display-popup-only-once-per-visit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!