为时事通讯模式弹出窗口添加cookie

问题描述 投票:0回答:1

我是asp.net的初学者,我想为我的网站弹出时事通讯

该弹出窗口已成功完成,并显示在网站的右下方。现在,我想添加一个cookie,以便当访问者单击“注册”按钮或关闭按钮时,弹出窗口不再显示(从不显示)。

但是在这里我被困住了,不知道如何开始添加cookie ...

这是我的代码:

<!-- Popup newsletter start -->

    <div class="dialog" title="Save time, save money!">
        <form>
            <p>Sign up and we'll send you the best deals</p>
                <input type="email" id="email" name="email" value ="Enter your e-mail address here">
                <input type="submit" value="Sign up">
        </form>
    </div>

    <script type="text/javascript">
        jQuery(document).ready(function () {
        jQuery(".dialog").dialog({
            bgiframe: true, autoOpen: true, height: '150', width: '350', modal: false, 
            position: {
                my: "right bottom",
                at: "right bottom",
            },
            create: function (event) {
                $(event.target).parent().css({ 'position': 'fixed'});
            }
        });
    });
    </script>


<!-- Popup newsletter end -->

Picture newsletter-popup

javascript modal-dialog popup newsletter jquery-cookie
1个回答
0
投票
jQuery(document).ready(function () {
    if(getCookie('popup_shown') == null) {
        jQuery(".dialog").dialog({
            bgiframe: true, autoOpen: true, height: '150', width: '350', modal: false, 
            position: {
                my: "right bottom",
                at: "right bottom",
            },
            create: function (event) {
                $(event.target).parent().css({ 'position': 'fixed'});
            }
        });
    }
});

jQuery('.close-button').on('click', setPopup_shown );
jQuery('form').on('submit', setPopup_shown);

function setPopup_shown() {
    setCookie('popup_shown', 'true');
}

function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
© www.soinside.com 2019 - 2024. All rights reserved.