Jquery 关闭前不在jGrowl工作?
我有以下代码,它从ASP.NET页面提取json数据,并将这些数据显示为通知。代码还将记录所完成的操作,并将其存储在数组中,以防止在同一会话中再次显示 我现在正在尝试实现这样的功能,当用户关闭一条消息时,它的ID会记录在cookie中,以防止再次显示它。为此,我尝试在beforeClose事件触发时写入cookie 除了保存一点饼干外,其他一切都很好。我的代码有什么问题吗Jquery 关闭前不在jGrowl工作?,jquery,cookies,Jquery,Cookies,我有以下代码,它从ASP.NET页面提取json数据,并将这些数据显示为通知。代码还将记录所完成的操作,并将其存储在数组中,以防止在同一会话中再次显示 我现在正在尝试实现这样的功能,当用户关闭一条消息时,它的ID会记录在cookie中,以防止再次显示它。为此,我尝试在beforeClose事件触发时写入cookie 除了保存一点饼干外,其他一切都很好。我的代码有什么问题吗 var alreadyGrowled = new Array(); var noteCookie = $.cookie("n
var alreadyGrowled = new Array();
var noteCookie = $.cookie("notificationsViewed");
if (noteCookie != null) { alreadyGrowled = noteCookie.split(","); }
function growlCheckNew() {
$.getJSON('getNotifications.aspx', function(data) {
$(data).each(function(entryIndex, entry) {
var newMessage = true;
$(alreadyGrowled).each(function(index, msg_id) {
if (entry['ID'] == msg_id) {
newMessage = false;
}
});
if (newMessage == true) {
$.jGrowl(entry['Message'], {
sticky: true,
header: entry['Title'],
beforeClose: function(e, m) {
$.cookie("notificationsViewed", entry['ID']);
}
});
}
alreadyGrowled.push(entry['ID']);
});
});
}这是:
$.jGrowl(entry['Message'], {
sticky: true,
header: entry['Title'],
beforeClose: function(e, m) {
$.cookie("notificationsViewed", entry['ID']);
}
});
需要:
var id = entry['ID'];
$.jGrowl(entry['Message'], {
sticky: true,
header: entry['Title'],
beforeClose: function(e, m) {
$.cookie("notificationsViewed", id);
}
});
beforeClose将在稍后发生,并带有所创建元素的上下文,但此时
条目
不可用,您可能会看到cookie被设置为未定义或出错…这应该通过显式地将id传递到闭包中来解决此问题。确定。使用close事件,我可以将消息的id添加到cookie中,我的下一个问题是,尽管我设置了过期时间,但cookie在新会话中似乎为null/空
//notification functions
var alreadyGrowled = new Array();
var keepClosed = new Array();
var noteCookie = $.cookie("notificationsViewed");
if (!noteCookie) {
$.cookie("notificationsViewed", "", { expires: 365, path: '/' });
} else {
keepClosed = noteCookie.split(",");
}
function growlCheckNew() {
$.getJSON('getNotifications.aspx', function(data) {
$(data).each(function(entryIndex, entry) {
var newMessage = true;
$(alreadyGrowled).each(function(index, msg_id) {
if (entry['ID'] == msg_id) {
newMessage = false;
}
});
$(keepClosed).each(function(index, msg_id) {
if (entry['ID'] == msg_id) {
newMessage = false;
}
});
if (newMessage == true) {
var id = entry['ID'];
alreadyGrowled.push(id);
$.jGrowl(entry['Message'], {
sticky: true,
header: entry['Title'],
close: function(e, m) {
keepClosed.push(id);
$.cookie("notificationsViewed", keepClosed);
}
});
}
});
});
}
通过选项传递您的Id参数。感谢您的帮助。很遗憾,这似乎不起作用,因为在刷新页面时,我仍然收到相同的两个通知。在FireBug中调试显示数组包含通知的两个id,但noteCookie变量为null。刚刚在作者站点()尝试了示例5,我注意到如果允许通知自动关闭自身,beforeClose事件似乎会触发。如果您通过x选项手动关闭它,则不会触发事件!
$.jGrowl(entry['Message'], {
myId:entry['ID'],
sticky: true,
header: entry['Title'],
close: function(e, m, o) {
$.cookie("notificationsViewed", o.myId);
}
});