簡體   English   中英

如何在jQuery模態中創建cookie?

[英]How to create cookie in jQuery modal?

我嘗試用jquery模態和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});
            });

        });

當我應該使用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;
}());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM