每个用户仅显示一次弹出窗口

这个问题已经有了答案,但是我仍然不确定它是如何工作的。


我在footer.php中使用以下HTML:


<div id="popup">

    <div>

        <div id="popup-close">X</div>

            <h2>Content Goes Here</h2>

    </div>

</div>

以及以下Javascript:


$j(document).ready(function() {

    $j("#popup").delay(2000).fadeIn();

    $j('#popup-close').click(function(e) // You are clicking the close button

    {

    $j('#popup').fadeOut(); // Now the pop up is hiden.

    });

    $j('#popup').click(function(e) 

    {

    $j('#popup').fadeOut(); 

    });

});

一切都很好,但是我只想对每个用户显示一次弹出窗口(也许使用所有论坛帖子都使用的cookie内容),但是我不知道确切如何将其合并到上面的JS中。


我知道我将必须在此页脚中加载cookie JS:


<script type="text/javascript" src="scripts/jquery.cookies.2.2.0.min.js"></script> 

但这就是我的全部理解,任何人都可以告诉我添加了Cookie的东西后JS / jQuery的外观如何吗?


谢谢


詹姆士


富国沪深
浏览 617回答 3
3回答

慕尼黑8549860

*注意:由于数据存储在浏览器内存中,因此每个浏览器都会显示一次弹出窗口。试试HTML localStorage。方法 :localStorage.getItem('key');localStorage.setItem('key','value');$j(document).ready(function() {&nbsp; &nbsp; if(localStorage.getItem('popState') != 'shown'){&nbsp; &nbsp; &nbsp; &nbsp; $j("#popup").delay(2000).fadeIn();&nbsp; &nbsp; &nbsp; &nbsp; localStorage.setItem('popState','shown')&nbsp; &nbsp; }&nbsp; &nbsp; $j('#popup-close, #popup').click(function(e) // You are clicking the close button&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $j('#popup').fadeOut(); // Now the pop up is hiden.&nbsp; &nbsp; });});

繁华开满天机

本示例使用jquery-cookie检查cookie是否存在并且尚未过期-如果其中任何一个失败,则显示弹出窗口并设置cookie(半伪代码):if($.cookie('popup') != 'seen'){&nbsp; &nbsp; $.cookie('popup', 'seen', { expires: 365, path: '/' }); // Set it to last a year, for example.&nbsp; &nbsp; $j("#popup").delay(2000).fadeIn();&nbsp; &nbsp; $j('#popup-close').click(function(e) // You are clicking the close button&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $j('#popup').fadeOut(); // Now the pop up is hiden.&nbsp; &nbsp; });&nbsp; &nbsp; $j('#popup').click(function(e)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $j('#popup').fadeOut();&nbsp;&nbsp; &nbsp; });};

慕沐林林

您可以使用php解决此问题。您仅在首页加载时回显弹出窗口的代码。另一种方法是设置cookie,它基本上是一个位于浏览器中的文件,其中包含某种数据。在第一页加载中,您将创建一个cookie。然后,随后的每个页面都会检查您的cookie是否已设置。如果已设置,则不显示弹出窗口。但是,如果未设置,则设置cookie并显示弹出窗口。伪代码:if(cookie_is_not_set) {&nbsp; &nbsp; show_pop_up;&nbsp; &nbsp; set_cookie;}
打开App,查看更多内容
随时随地看视频慕课网APP