3

I'm using the jQuery Cookie plugin (https://github.com/carhartl/jquery-cookie) and I'm having issues persisting my cookies across different pages on my website.

I have a piece of content that displays on each page and once hidden it sets the cookie like so (Apologies for all of the variables, they are not at all relevent :

// Binds the close event to the button.
$alert.on('click', function(e) {
    $alertWrap.fadeOut();

    // Sets the breaking-delete cookie to yes.
    $.cookie('breaking-bar-delete', 'yes', {expires: 7 });
});

When the initial script fires it checks to see if the cookie exists:

// If the current bar is not supressed and they are not in the editor, and they do not have a closed cookie it will setup the bar.
if ($.cookie('breaking-bar-delete') == undefined) {
  $alert.css("display","block");
}

// If there's no news, or they have the closed cookie for the current bar it hides it by default.
if ($.cookie('breaking-bar-delete') == 'yes') {
  $alert.parent().css("display","none");
}

Now this works for the path that you hide the bar on so it doesn't re-display if you refresh. But if you go to a part of the site with a different path it doesn't detect the cookie and displays the content.

Is there some sort of configuration I can set when I initially serve the cookie so it persists throughout all of the pages on my site?

James Ives
  • 3,177
  • 3
  • 30
  • 63
  • Possible duplicate of [Cookie path and its accessibility to subfolder pages](http://stackoverflow.com/questions/576535/cookie-path-and-its-accessibility-to-subfolder-pages) – Noy Feb 16 '16 at 13:39

1 Answers1

5

Path:-

path

path: '/' Define the path where the cookie is valid.

By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior).

If you want to make it available for instance across the entire domain use path: '/'.

Default: path of page where the cookie was created.

So, for your cookie:-

$.cookie('breaking-bar-delete', 'yes', { expires: 7, path: '/' });
Community
  • 1
  • 1
BenG
  • 14,826
  • 5
  • 45
  • 60