每个站点访问弹出框一次出现

我有一个弹出框出现在我的网站上,每次访问或刷新主页时都会出现。 我设法使用……

var firstTime = localStorage.getItem("firstTime"); if(! firstTime){ localStorage.setItem("firstTime", true); } }); 

…让它只出现一次,但有人知道如何每次访问网站(而不是页面)时出现一次吗? 这样,当你回到网站时它就不会出现。 任何帮助都会很棒! 完整代码如下……

   $(document).ready(function() { var firstTime = localStorage.getItem("firstTime"); if(! firstTime){ var id = '#dialog'; //Get the screen height and width var maskHeight = $(document).height(); var maskWidth = $(window).width(); //Set height and width to mask to fill up the whole screen $('#mask').css({'width':maskWidth,'height':maskHeight}); //transition effect $('#mask').fadeIn(500); $('#mask').fadeTo("slow",0.9); //Get the window height and width var winH = $(window).height(); var winW = $(window).width(); //Set the popup window to center $(id).css('top', winH/2-$(id).height()/2); $(id).css('left', winW/2-$(id).width()/2); //transition effect $(id).fadeIn(500); //if close button is clicked $('.window .close').click(function (e) { //Cancel the link behavior e.preventDefault(); $('#mask').hide(); $('.window').hide(); }); //if mask is clicked $('#mask').click(function () { $(this).hide(); $('.window').hide(); }); localStorage.setItem("firstTime", true); } });  //rest of html content  
X


Subscribe

to receive email updates

您将不得不检查上次访问页面的时间,通常30分钟会话是常见的(尽管没有足够的数据来支持这一点)

因此,请在localstorage中设置访问时间

 localStorage.setItem("visitTime", new Date().getTime() ); 

并且下次检查此值,无论是在30分钟之前(或安全的60分钟之前)还是30分钟之后(或60分钟之后)

为您更新了这个小提琴 。 截至目前,我已将间隔时间设置为1分钟,以便您可以对其进行测试。

 var timeIntervalToAssumeNewSiteVisit = 1*60*1000; //in milliseconds $(document).ready(function() { var currentTime = new Date().getTime(); var lastVisitTime = parseInt( localStorage.getItem("lastVisitTime") ); if( isNaN( lastVisitTime ) || ( currentTime - lastVisitTime ) >= timeIntervalToAssumeNewSiteVisit ) { alert( "new visit" ); } else { alert( "same visit, old session" ); } localStorage.setItem("lastVisitTime", currentTime); });