2012-08-07 54 views
0

我想添加cookie到我的模式對話框,但我不知道如何。我想在24小時內添加cookies,有誰可以幫忙嗎?這裏是我的模態對話框代碼:如何在jquery模式對話框中設置cookie

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script> 
<script type="text/javascript" href="/jquery/jquery.cookies.js"></script> 

<script> 
$(function() { 
     // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore! 
     if ($.cookie('showDialog') == undefined || $.cookie('showDialog') == null || $.cookie('showDialog') != 'false') { 

     $("#dialog:ui-dialog").dialog("disable"); 
     $(".selector").dialog({ hide: "slide" }); 
     $("#dialog-modal").dialog({ 
      width:860, 
      height: 420, 
      modal: true, 
      resizable:false, 
      draggable:false 
     }); 
     $.cookie('showDialog', 'false', { expires: 1 }); // set the cookie, with expiry after 1 day 
} 
    }); 

    </script> 



<link href="http://xxx.com/jquery/jquery.ui.all.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 

<div id="dialog-modal" title="Like Us on Facebook"> 
    <div class="fb-like-box" data-href="http://www.facebook.com/xxx" data-width="820" data-height="335" data-show-faces="true" data-stream="false" data-header="false"></div> 
</div> 
+1

您的代碼看起來不錯。你有沒有包含jQuery cookies腳本? https://github.com/carhartl/jquery-cookie/ – 2012-08-07 09:38:14

回答

2

你的代碼本身看起來不錯,但你也應該包括script參考jQuery的cookies plugin,因爲它不是jQuery的標準的一部分。

<script type="text/javascript" href="/scripts/jquery.cookies.js"></script> 

您還需要設置cookie你顯示對話框後:

if ($.cookie('showDialog') == undefined || $.cookie('showDialog') == null || $.cookie('showDialog') != 'false') { 
    // show dialog... 
    $.cookie('showDialog', 'false', { expires: 1 }); // set the cookie, with expiry after 1 day 
} 
+0

非常感謝! – androider 2012-08-07 10:44:53

+0

我有一個問題,現在窗口不像模態對話框,但它在我的頁面上方我的標題上...我在上面的問題中編輯了代碼! 感謝您的幫助,Rory McCrossan – androider 2012-08-07 10:56:35