如何在jquery模态对话框中设置cookie [英] How to set cookies in jquery modal dialog

查看:166
本文介绍了如何在jquery模态对话框中设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的模态对话框中添加Cookie,但我不知道如何。我想添加cookie为24小时,可以有人帮助吗?这是我的modal对话框代码:

I would like to add cookies to my modal dialog, but I don't know how. I would like to add cookies for 24hours, can anybody help please? Here is my code for modal dialog:

    <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>


推荐答案

您的代码本身看起来不错, 脚本对jQuery Cookie插件的引用,因为它不是jQuery的标准部分。

Your code itself looks fine, however you should also include a script reference to the jQuery cookies plugin, as it is not a standard part of jQuery.

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

您还需要在显示对话框后设置cookie:

You also need to set the cookie after you display the dialog:

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
}

这篇关于如何在jquery模态对话框中设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆