【问题标题】:Add cookie for newsletter-modal-popup为 newsletter-modal-popup 添加 cookie
【发布时间】:2020-06-15 00:00:59
【问题描述】:

我是 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】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      相关资源
      最近更新 更多