Cookie if 语句无法正常工作
Cookie if statement does’t work correctly
>目标
我正在使用来自 carhartl 的轻量级 cookie 插件。我正在尝试的是当用户单击通知栏的关闭按钮时设置cookie。当用户关闭此消息时,应隐藏该栏,直到时间到期。
问题所在
下面是我的脚本。因此,当用户点击类.notification-bar
栏时,应该隐藏栏并设置cookie。如果未找到 cookie,请显示该栏,直到时间到期。
不幸的是,下面的代码不起作用。我没有收到错误消息,但是当我关闭通知栏并刷新页面时,该消息将再次出现。在此之后不会设置cookie。
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网站上发布新帖子时,让通知栏工作的代码如下所示:
函数.php文件
当有新帖子发布时,钩入WordPress的transition_post_status
功能。
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 );
和标头.php文件
<?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
}
}
?>
除了错误的右引号之外,您还有几个小问题:
- 您缺少在 cookie 存在时隐藏
notification-bar
的逻辑。 - 似乎
expires
值必须是数字而不是字符串。
编辑:片段工具不想很好地使用cookie,所以请参阅工作小提琴
$(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: '/'});
});
});
#notification-bar {
height: 50px;
width: 200px;
background-color: blue;
}
<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" />
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- 最大高度转换不;不工作,工作缓慢
- Gulp-rev:不是第一次工作,而是在第一次工作之后工作
- JQuery - Ajax: encodeUriComponent不工作(EncodeUri工作)