【问题标题】:How to show popup only once at 2 days in javascript?如何在javascript中仅在两天内显示一次弹出窗口?
【发布时间】:2015-09-17 03:03:49
【问题描述】:

我需要一个简单的代码来使用 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>

提前致谢。

【问题讨论】:

  • java 还是 javascript ??
  • hmmm :) ...因为我不知道 java 或 javascript,我认为代码是用 javascript 编写的,但不确定:(.
  • 嗯,您的代码是用 javascript 编写的。为什么要使用 cookie?
  • 谢谢你告诉我 :) ...好吧.. 因为我在一些网站上看到你可以使用 cookie 来做到这一点,我猜。
  • 你还有其他想法吗?!

标签: javascript html cookies popupwindow


【解决方案1】:

我的解决方案使用 cookie 工作。

把这个放在你页面的&lt;head&gt;,以确保jQueryjQuery UI(用于显示对话)和jQuery Cookie Plugin库被加载:

<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>

将其放在页面底部&lt;/body&gt; 标记之前

$(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>

将其放在页面的 &lt;body&gt; 部分,并放置您想要的任何东西,而不是 &lt;p&gt; 的东西。

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

如果您有任何问题,请发布您页面的链接或 html 代码,我会帮助您插入此代码。我已经测试过了,它可以工作。

下面是实际的代码示例: https://jsfiddle.net/d2s1r0mp/1/

【讨论】:

  • 对我来说……这是需要显示的 div:#bsadsheadline。如何使用您的代码更新我的代码以使其正常工作?谢谢。
  • 这是专门为您的#bsadsheadline 更正的一个:codeshare.io/Tq7Tr。确保在 &lt;head&gt; 之后添加 2 行,并且没有重复的行(即 jQuery 不包含两次,以防您之前在 HTML 中包含它)。
  • 我发布的内容有效。你能发布你的完整页面代码吗?
  • 现在检查。我已经包含了 jquery 并设置好了。
  • 很抱歉您在实施我的解决方案时遇到问题。刚看了你的代码。您网站的代码结构非常混乱,需要一些工作来修复!
【解决方案2】:

查看此代码...

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。

【讨论】:

  • 我不知道如何将其放入我的代码中 :( 。P.S。我不希望这仅适用于我网站上的用户。我希望它适用于所有访问者。跨度>
  • 在您的脚本文件或内联脚本标签中使用上述代码,确保您的 div#popup 设置为 display:none,此代码不限于用户,因此它适用于所有访问者..
【解决方案3】:

将此代码添加到页面头部就可以了:

<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 帮助我。

【讨论】:

    【解决方案4】:

    下面的代码会在您的浏览器中打开一个窗口。

    window.open("url"); 
    

    就每 2 天打开一次而言,最好的办法是将其包装在某种 cron 作业中。不知道为什么要使用 cookie 来打开窗口。

    【讨论】:

    • 我想使用 cookie 来跟踪用户的时间,如果该用户第二天访问我的页面,我不希望他再次看到弹出窗口,仅在 2 天后。我想更新我已经拥有的代码。我试过这个:if($.cookie('popup') != 'seen'){ $.cookie('popup', 'seen', { expires: 365, path: '/' }); // Set it to last a year, for example. 但它没有用。
    猜你喜欢
    • 2017-08-13
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 2023-03-11
    相关资源
    最近更新 更多