首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cookie if语句不能正常工作

Cookie if语句不能正常工作
EN

Stack Overflow用户
提问于 2014-12-10 05:16:38
回答 1查看 156关注 0票数 0

目标

我使用的是carhartl的轻量级cookie插件。我正在尝试设置一个cookie,当用户单击通知栏的关闭按钮时。当用户关闭此消息时,它的栏应该被隐藏,直到时间到期。

问题所在

下面是我的脚本。因此,当用户单击类cookie时,栏应该是隐藏并设置.notification-bar。如果找不到cookie,则显示该条,直到时间到期。

不幸的是,下面的代码不能工作。我没有收到错误消息,但当我关闭通知栏并刷新页面时,该消息将再次出现。在此之后未设置cookie。

代码语言:javascript
复制
var notifyBar = $('#notification-bar’)

                if($.cookie('demo_cookie') == null) {
                    notifyBar.css( "display", "block" );
                };

                $(".notification-close").click(function() {
                    notifyBar.css( "display", "none" );
                    $.cookie('demo_cookie', 'Demo Cookie', { expires: '<?php $expiry_date ?>', path: '/'}); 
                });

更新

JSFIDDLE:http://jsfiddle.net/cddhez75/8/

当WordPress站点上发布了新帖子时,让通知栏正常工作的代码如下所示:

Functions.php文件

当有新发布的帖子时,WordPress的transition_post_status函数中的钩子。

代码语言:javascript
复制
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
    //Check if our post status then execute our code
    if ( $new_status == 'publish' && $old_status != 'publish' ) {
        if ( get_option( 'new_post_notification' ) !== false ) {
            // The option already exists, so we just update it.
            update_option( 'new_post_notification', $post );

        } else {
            add_option( 'new_post_notification', $post );

        }
    }

}, 10, 3 );

和Header.php文件

代码语言:javascript
复制
<?php 

// Get the new_post_notification which holds the newest post
$notification = get_option( 'new_post_notification' );

if( false != $notification ) {

    //Get the post's gmt date. This can be changed to post_date
    $post_date = strtotime( $notification->post_date_gmt );

    //Get the current gmt time
    $todays_date = current_time( 'timestamp', true );

    //Set the expiry time to two days after the posts is published
    $expiry_date = strtotime('+10 minute', $post_date);


    if( $expiry_date > $todays_date ) { 
        // Display your notification if two days has not been passed
        //echo 'New post published';


    ?>
        <script type="text/javascript">
            $(document).ready(function(){
                var notifyBar = $('#notification-bar');

                if($.cookie('demo_cookie') == null) {
                    notifyBar.css( "display", "block" );
                };

                $(".notification-close").click(function() {
                    notifyBar.css( "display", "none" );
                    $.cookie('demo_cookie', 'Demo Cookie', { expires: '<?php $expiry_date ?>', path: '/'}); 
                                        console.log('hello');

                });


            });

        </script>

        <div id="notification-bar-spacer">
            <div id="notification-bar" class="wpfront-fixed">
                <div class="notification-close">X</div>
                <table border="0" cellspacing="0" cellpadding="0">
                    <tbody>
                        <tr>
                            <td>
                                <div class="wpfront-message">
                                    Geachte leden, er zijn één of meerdere nieuwe berichten toegevoegd. <a href="http://wordpress.devrijheidbussum.nl/wordpress/login/">Ga naar login pagina ></a>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>


    <?php

    }

}
?>
EN

回答 1

Stack Overflow用户

发布于 2014-12-10 05:45:21

除了错误的闭合引号,你还有几个小问题:

您缺少的cookie

  • 是在cookie存在时隐藏notification-bar的逻辑。

  • 似乎expires值必须是数字而不是字符串。

编辑:代码片段工具不想很好地处理cookies,因此请参阅

代码语言:javascript
复制
$(function() {
    var notifyBar = $('#notification-bar');
    
    if($.cookie('demo_cookie') == null) {
        notifyBar.css( "display", "block" );
    } else {
        notifyBar.css( "display", "none" );
    }
    
    $(".notification-close").click(function() {
        notifyBar.css( "display", "none" );
        $.cookie('demo_cookie', 'Demo Cookie', { expires: 7, path: '/'}); 
    });
});
代码语言:javascript
复制
#notification-bar {
    height: 50px;
    width: 200px;
    background-color: blue;
}
代码语言:javascript
复制
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="notification-bar"></div>
<input type="button" class="notification-close" value="Close" />

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27388986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档