duanhe3393 2015-09-22 16:05
浏览 97

jQuery cookie弹出窗口,每个会话只显示一次

I use this code to show pop-up to users only once per session. It works but it will show once only on a specific URL. For example, if a user visits www.mydomain.com/some-url-1 it will show only once on that URL, but if a user visits a different URL www.mydomain.com/some-different-url the pop up will show up again.

I want it to show only once across whole domain instead.

Here is the code:

<div id="trex-widget391" class="trex-popup-widget"></div> 
   <script>
       function trexCallback391(trex){document.getElementById("trex-widget391").innerHTML=trex.html;}
       jQuery(".trex-popup-widget").mouseover(function(e){
           if(document.cookie.indexOf("popup_trex") ===-1) {
               jQuery("#trex_overlay_fsl_popup").fadeIn('slow');
               expiry = new Date();expiry.setTime(expiry.getTime()+(10*60*1440000)); 
               document.cookie = "popup_trex=yes; expires=" + expiry.toGMTString();
           }
       });
   </script> 
   <script src="http://clanci.geek.hr/widget/widget.php?id=391" async defer></script>

Does anyone know what needs to be modified to show pop-up only once across whole domain and its subdomains?

  • 写回答

1条回答 默认 最新

  • dongmen1860 2015-09-22 16:14
    关注

    If your list of supported browsers allows, it would probably be best to use Web Storage to track whether your user has seen the pop-up in a whole session.

    https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API

    sessionStorage would allow you to store a value to signify that the user has seen the pop-up. This storage is sandboxed to your domain, and is cleared at the end of the session.

    Your snippet would need to be modified to something like:

    <div id="trex-widget391" class="trex-popup-widget"></div> 
       <script>
           function trexCallback391(trex){document.getElementById("trex-widget391").innerHTML=trex.html;}
           jQuery(".trex-popup-widget").mouseover(function(e){
               if(!sessionStorage.seenPopup) {
                   jQuery("#trex_overlay_fsl_popup").fadeIn('slow');
                   sessionStorage.seenPopup = "yes";
               }
           });
       </script> 
       <script src="http://clanci.geek.hr/widget/widget.php?id=391" async defer></script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog