簡體   English   中英

如何在javascript中每2天只顯示一次彈出窗口?

[英]How to show popup only once at 2 days in javascript?

我需要一個簡單的代碼來為每個使用 cookie 的用戶每 2 天顯示一次彈出窗口。 如果有人可以幫助我,我不知道用 javascript 編碼。

我的彈出窗口的代碼是:

<script type="text/javascript"> 
function getValue()
{
document.getElementById("bsadsheadline").style.display = 'none';
}

$(document).ready(function () {
    setTimeout(function(){
        $('#bsadsheadline').fadeIn(500);
    }, 30000);
});
</script>
<div id="bsadsheadline">
<div id="bloggerspicesflotads">
<span style="color:#fff;font-size:12px;font-weight:bold;text-shadow:black 0.01em 0.01em 0.01em"></span>
<span style="color:#fff;font-size:12px;font-weight:bold;text-shadow:black 0.01em 0.01em 0.01em;float:right;padding-top:2px;padding-right:115px"><a href="javascript:void(0);" onclick="getValue();">inchide(x)</a></span>
</div>
<div id="bsadsbase">
my code
</div></div>

提前致謝。

我的解決方案使用 cookie。

將其放在頁面的<head>中,以確保加載了jQueryjQuery UI (用於顯示對話)和jQuery Cookie 插件庫:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>

將此放在頁面底部的</body>標記之前

$(document).ready(function() {

   // Make sure dialog is initially hidden:
   $('#dialog').dialog({autoOpen: false});

    // Check for the "whenToShowDialog" cookie, if not found then show the dialog and save the cookie.
    // The cookie will expire and every 2 days and the dialog will show again.

    if ($.cookie('whenToShowDialog') == null) {

        // Create expiring cookie, 2 days from now:
        $.cookie('whenToShowDialog', 'yes', { expires: 2, path: '/' });

        // Show dialog
        $('#dialog').dialog("open");        
    }

});
</script>

把它放在頁面的<body>部分,並放置任何你想要的而不是<p>東西。

<div id="dialog" title="Test dialog">
  <p>This dialog will only show every 2 days.</p>
</div>

如果您有任何問題,請發布您頁面的鏈接或 html 代碼,我會幫助您插入此代碼。 我已經測試過它並且它有效。

這是正在運行的代碼示例: https : //jsfiddle.net/d2s1r0mp/1/

看看這個代碼...

 if(localStorage.last){ if( (localStorage.last - Date.now() ) / (1000*60*60*24*2) >= 1){ //Date.now() is in milliseconds, so convert it all to days, and if it's more than 1 day, show the div document.getElementById('div#popup').style.display = 'block'; //Show the div localStorage.last = Date.now(); //Reset your timer } } else { localStorage.last = Date.now(); document.getElementById('div#popup').style.display = 'block'; //Show the div because you haven't ever shown it before. }

為了完全准確,您必須使用數據庫來維護向用戶顯示的用戶 ID 和塊時間。 但是,如果它不是一個非常嚴格和快速的規則,您可以通過將時間存儲在瀏覽器 cookie 中來獲得近似結果,並基於此顯示/隱藏 div。

將此代碼添加到頁面的頭部可以完成以下工作:

<script>
$(document).ready(function() {

    // Check for the "whenToShowDialog" cookie, if not found then show the dialog and save the cookie.
    // The cookie will expire and every 2 days and the dialog will show again.
    if (jQuery.cookie('whenToShowDialog') == null) {

        // Create expiring cookie, 2 days from now:
        jQuery.cookie('whenToShowDialog', 'yes', { expires: 2, path: '/' });

        // Show dialog
        setTimeout(function(){
        $('#bsadsheadline').fadeIn(500);
    }, 30000);       
    }

});
</script>

感謝@John Valai 幫助我。

下面的代碼在瀏覽器中打開一個窗口。

window.open("url"); 

就每 2 天打開一次而言,最好的辦法是將其包裝在某種 cron 工作中。 不知道為什么要使用 cookie 來打開窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM