user2350789
user2350789

Reputation: 47

Creating a cookies notice

I'm trying to create a cookies notice for first time users on my website. I'm using the jQuery tool 'jQuery.cookie.js' and have a panel that slides up. When the user agrees to the notice, a class is removed from the body and from then on, the message shouldn't slide up again. However, even after agreeing to the message, the panel continues to slide up. My code is as follows...

if ($('body.unaccepted').length > 0) {
setTimeout(function(){ 
    $("#cookies_pane").slideToggle(900);
}, 2400);
}

$('#cookies_accept a').click(function() {
$("#cookies_pane").slideToggle(900);
    $.cookie('visited', 'yes', {expires: 30, path: '/'});
    setTimeout(function(){ 
        $(this).addClass("hidden");
    }, 900);
}); 

var visited = jQuery.cookie('visited');
if (visited == 'yes') {
$('body').removeClass('unaccepted');  
};

Any help would be much appreciated!

Upvotes: 0

Views: 195

Answers (2)

cirrus
cirrus

Reputation: 5662

Don't you need to move this block of code to the top?

var visited = jQuery.cookie('visited');
if (visited == 'yes') {
$('body').removeClass('unaccepted');  

Here's a working jsfiddle: http://jsfiddle.net/h66kw/2/

You might alsowant to send the cookies back from the server.

Upvotes: 0

ThermalCube
ThermalCube

Reputation: 168

I think your cookie expires instantly. You should use:

var time = new Date()
var exp = time.setTime(time.getTime() + (30*24*60*60*1000));
$.cookie('', '', { expires: exp });

Upvotes: 1

Related Questions