javascript - jQuery 对话框弹出 Cookie

标签 javascript jquery cookies

我需要这个弹出窗口只为每个访问者显示一次。当用户单击关闭按钮时,cookie 应触发并将弹出窗口设置为 30 天内不显示。我尝试自己安装 cookie,但没有成功,因为我对 JavaScript 的了解有限。我在这里阅读了几篇与此相关的帖子,但它们对我没有帮助。

JavaScript:

<link rel="stylesheet" href="jquery-ui-1.10.3.custom/jquery-ui-1.10.3.custom.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function() {
$( "#dialog-modal" ).dialog({
height: 380,
width: 500,
modal: true,
buttons: {
    Ok: function() {
        $( this ).dialog( "close" );
        }
    }
});
});
</script>

HTML:

<div id="dialog-modal" title="Please Note:" class="content-list">
    <p>If you are taking advantage of our 21 day risk free trial <strong>your credit card will not be charged for 21 days</strong> after you receive your new biofeedback headband.</p>
    <ul>
        <li>Only Available for residents of the USA</li>
        <li>No Risk - 100% Money-Back Guarantee</li>
        <li>If you’re not satisfied we even pay for your return shipping</li>
    </ul>
</div>

谢谢。

最佳答案

您可以使用jquery cookie plugin 。如果您包含该库,则可以执行以下操作:

$(function () {
    if (!$.cookie("notice-accepted")) {
        $("#dialog-modal").dialog({
            height: 380,
            width: 500,
            modal: true,
            buttons: {
                Ok: function () {
                    $.cookie("notice-accepted", 1, { expires : 30 });
                    $(this).dialog("close");
                }
            }
        });
    }
});

注意:您需要添加 style="display: none;"到您的对话框 <div>因此,当您不打开对话框时,它不会显示。

Demo on JSFiddle

关于javascript - jQuery 对话框弹出 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21637405/

相关文章:

javascript - jQuery slider 值位于 slider 上方

javascript - 从 Electron Container IPC Channel 接收数据时,变更检测会间歇性工作

javascript - 单击后更新 HTML 内容

javascript - jquery:当页面= 1时隐藏按钮

javascript - 动态加载脚本会改变其行为

javascript - 循环播放 Bootstrap Popover

javascript - SVG : how to properly handle mouseover and mouseout event?

asp.net-mvc-2 - iframe、跨域 cookie、p3p 策略和 safari 出现错误 : A required anti-forgery token was not supplied or was invalid

php - session cookie 设置 SameSite=None;安全的;

javascript - 未使用 javascript fetch 设置 Cookie