为jQuery UI模式对话框设置Cookie

时间:2018-05-07 19:11:30

标签: jquery wordpress jquery-ui modal-dialog

我制作了一个自定义的jQuery模式/弹出窗口并将其添加到我的wordpress网站。 这很有效。

现在我想要一个访问者,当他点击两个按钮中的一个保存cookie时,如果还不知道cookie,则只显示弹出窗口。 我尝试了几个选项和线程,例如,jQuery cookie插件,js-cookie插件等。但是从未成功完成设置。

我的代码:

JS

$(document).ready(function () {

    //generate dialog (modal)
    jQuery("#dialogForm").dialog({
        modal: true,
        resizable: false,
        draggable: false,
        width: 730,
        height: 490,
        show: {
            effect: "blind",
            duration: 800

        }
    });

    //hide title bar
    jQuery(".ui-dialog-titlebar").hide();

    //close on press of 'Contacteer Ons' button
    jQuery('#contact').click(function (e) {
        e.preventDefault();
        jQuery('#dialogForm').dialog('close');
        $('#dialog-container').remove();
    });

    //close on press of 'Sluiten' button
    jQuery('#close').click(function (e) {
        e.preventDefault();
        jQuery('#dialogForm').dialog('close');
        $('#dialog-container').remove();
    });

    //close on ESC key
    $(document).keydown(function (event) {
        if (event.keyCode == 27) {
            jQuery('#dialogForm').dialog('close');
            $('#dialog-container').remove();
        }
    });

});

HTML

//pop-up
function boeckske_enqueue_scripts(){
    wp_enqueue_script( 'my-dialog', get_template_directory_uri() .'/js/popup.js', array( 'jquery' ), null, true );

    // If you saved your CSS as a file instead of using the Customizer:
    //wp_enqueue_style( 'my-dialog', get_stylesheet_directory_uri() .'/css/popup.css', array(), null );
}
add_action( 'wp_enqueue_scripts', 'boeckske_enqueue_scripts' );

//add HTML pop-up to footer
function boeckske_dialog_html(){ ?>
    <html>
        <head>
            <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
            <script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
            <link rel="stylesheet" href="/wp-content/themes/enfold/css/jquery-ui.css" />
            <script src="/wp-content/themes/enfold/js/jquery.cookie.js"></script>
        </head>
        <body>
            <div class="modal fade" id="myModal">       
                <div id="dialog-container">
                    <div id="dialogForm">
                    <form id="myform" method="post">
                        <br><br><br>
                        <p id=title name=title><b>VERLOF</b></p>
                        <br><br>
                        <p id=eerste name=eerste>Onze toonzaal zal op <b>5 mei, 10 mei,<br>11 mei en 21 mei</b> gesloten zijn.</p>
                        <p id=tweede name=tweede>Voor dringende interventies, gelieve te <u>mailen<br>of een bericht in te spreken</u>.</p><br>
                        <br/>
                        <input type="button" name="contact" id="contact" onclick="location.href='https://www.sano-tech.be/contact/';" value="Contacteer ons" />
                        <input type="button" name="close" id="close" value="Sluiten" />
                    </form>
                    </div>
                </div>
            </div>
        </body>
    </html>
<?php }
add_action( 'wp_footer', 'boeckske_dialog_html' );

对话

Click here

1 个答案:

答案 0 :(得分:0)

$(document).ready(function () {

    if ($.cookie('popup') == null) {
        //show dialog (modal)
        jQuery("#dialogForm").dialog({
            modal: true,
            resizable: false,
            draggable: false,
            width: 730,
            height: 490,
            show: {
                effect: "blind",
                duration: 800

            }
        });

        //hide title bar
        jQuery(".ui-dialog-titlebar").hide();
    } else {
        var x = document.getElementById("myModal");
        x.style.display = "none";
    }
});

//close on press of 'Contacteer Ons' button
jQuery('#contact').click(function (e) {
    e.preventDefault();
    jQuery('#dialogForm').dialog('close');
    $('#dialog-container').remove();
    //alert("adding cookie");
    $.cookie('popup', 'seen', { expires: 30 });
});

//close on press of 'Sluiten' button
jQuery('#close').click(function (e) {
    e.preventDefault();
    jQuery('#dialogForm').dialog('close');
    $('#dialog-container').remove();
    //alert("adding cookie");
    $.cookie('popup', 'seen', { expires: 30 });
});

//close on ESC key
$(document).keydown(function (event) {
    if (event.keyCode == 27) {
        jQuery('#dialogForm').dialog('close');
        $('#dialog-container').remove();
    }
});