jQuery对话框弹出Cookie

时间:2014-02-07 20:45:17

标签: 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>

感谢。

1 个答案:

答案 0 :(得分:5)

您可以使用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