简体   繁体   English

jQuery cookie弹出窗口在每个会话中仅显示一次

[英]jQuery cookie pop-up to show only once per session

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. 它可以工作,但只会在特定的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. 例如,如果用户访问www.mydomain.com/some-url-1 ,它将仅在该URL上显示一次,但是,如果用户访问其他URL www.mydomain.com/some-different-url则弹出窗口再次出现。

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? 有谁知道需要修改什么才能在整个域及其子域中仅显示一次弹出窗口?

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. 如果支持的浏览器列表允许,则最好使用Web存储来跟踪用户在整个会话中是否看到了弹出窗口。

https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API 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. sessionStorage允许您存储一个值,以表示用户已看到弹出窗口。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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