Cookiechoices cookie only set for specific page not whole website

Advertisements

When I first downloaded the cookiechoices.js file from the link suggested by Google and then installed it onto my site it didn’t really work correctly. It would display the message but when the user clicked on the ok button the Cookiechoices cookie only seemed to be getting set for that specific page, so when a second page was visited the cookie message would be displayed a second time. 

Advertisements

Cause of the Error

The reason for this was that the cookiechoices code was not setting the path correctly. You can see the Path that the cookie sets by going to “More tools->Developer tools” within Chrome and then selecting the Application tab and expanding Cookies within the Storage tree structure. As you can see in this example the path /abap has been included as part of the cookie properties. This is not what you want, it should just be a ‘/’ like all the others. In this case the cookie will only be set for all pages within the /abap directory rather than the whole website.

CookieChoices Path Error

CookieChoices Path Error

Advertisements

Solution

The solution is actually quite simple but took me a lot of messing around to find. You basically just need to hard code the path property to be the route  of your website. To do that within the cookie choices code recommended by google simply find the following section of code document.cookie = cookieName + ‘=y; expires=’ + expiryDate.toGMTString(); Now simply add “path=/” after =y; i.e.  Resultant JavaScript code will now look like this: document.cookie = cookieName + ‘=y; path=/; expires=’ + expiryDate.toGMTString();  

Advertisements

Test

Once you have implemented the above fix and uploaded the new file to you website try a page where the cookie choices message is displayed  and click the OK button again. The result should be that the cookie is set for the route of the website.

Advertisements
cookiechoices fix

cookiechoices fix

Advertisements

Leave a Comment: