【问题标题】:How to create cookie in jQuery modal?如何在 jQuery modal 中创建 cookie?
【发布时间】:2012-09-22 19:59:07
【问题描述】:

我尝试使用 jquery modal 和 cookie 创建一个页面。模态运行良好,但我在其中设置 cookie 时遇到问题。我想实现以下目标:

  • 在主页加载时,显示模式框(弹出)
  • 单击关闭按钮时弹出关闭,当用户返回页面时,模式框将不显示。
  • 弹出窗口将在 7 天后再次显示。

请指教!

        // Close modal when click on close button 
    $close.click(function(e){
    e.preventDefault();
    method.close();
    });

       return method;
    }());


        // querying the document
        $(document).ready(function(){

        if (document.cookie.indexOf('$close.click=true') == -1){
            var sevenDays = 1000*60*60*24*7;
            var expires = new Date((new Date()).valueOf() + sevenDays);
            document.cookie = "$close.click=true;expires=" +  expires.toUTCString();
            };

            // Append data via Ajax
            $.get('url', function(data){
                modal.open({content: data});
            });

        });

【问题讨论】:

    标签: javascript jquery ajax cookies modal-dialog


    【解决方案1】:

    当我应该使用 cookie 时,我会使用这个插件 https://github.com/carhartl/jquery-cookie

    您可以使用以下模型结构

    var modal = (function(){
            var 
            method = {},
            $overlay,
            $modal,
            $content,
            $close;
    
            // Center the modal in the viewport
            method.setPosition = function () {
                // Set Postion
            };
    
            // Open the modal, reveal overlay 
            method.open = function (settings) {
                if($.cookie('NameOfCookie'))
                {
                    // We dont need show popup
                    return
                }else{
                    // Open Modal               
                    $.cookie('NameOfCookie', 'created', { expires: 7 });
                    method.GenerateModalPopup();
                    method.setPosition()
                }
            };
    
            // Close the modal and overlay, then unbind the resize event when modal is closed
            method.close = function () { 
                // Close Modal
            };
    
            method.GenerateModalPopup=function(){
                // Generate the HTML and add it to the document
    
            };
    
            // Open popup window
            method.open();
    
       return method;
    }());
    

    【讨论】:

    • 感谢您的提示!我的朋友刚刚帮助了我,当我申请 if(!$.cookie('modal')) { $.get('url', function(data){ modal.open({content: data}); } );
    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2016-01-08
    相关资源
    最近更新 更多