1

I usually find an answer to all my questions, but not this time.

I'm trying to store a cookie using javascript on the client browser. Until 2 days ago everything was working perfectly but now my function in some page of my website sets the wrong path.

The function is the simple:

document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"

I thought that this is a generic function that make the cookie work in every pages of my website.

If i don't set the specific domain name or the path name the cookie have to be set for all of my website pages.

The problem is that in some pages for some reason that i can't understand the cookie automatically set a wrong path (the actual one), like /actualpage

This is a problem because as i get from stack overflow --> https://stackoverflow.com/a/18463119/10557173 if i set a cookie for a specific path, when the url path change the cookie is override and all the function that i made work only if the cookie is not setted start again to work. This is something that i want to avoid.

Does someone know how I can force the cookie to always set no path? /

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Labiles
  • 11
  • 1
  • Do you need to set a cookie? Do you need to send it with every request to the server? If not, use sessionStorage. If yes, use a well used cookie script. Your code does not set /actualpage if you have path=/ – mplungjan Dec 18 '19 at 09:29
  • @mplungjan yeah i know that my code do not have to set any path, this is why i can't understand this strange behavior. I don't really need to use cookie but i would like to understand the problem, can i use localStorage to keep the information on the client browser even on different session? – Labiles Dec 18 '19 at 09:40
  • Yes. localStorage is cross sessions. – mplungjan Dec 18 '19 at 09:40
  • @mplungjan ok thanks, i'll make it work with local storage. But do you have any thoughts about that strange behavior? – Labiles Dec 18 '19 at 09:43
  • No, I would need to see it in action. The code you posted will not do what you describe - possibly the expiry is set wrong and the path is ignored or the cvalue is not correctly escaped with same result – mplungjan Dec 18 '19 at 09:49
  • ok, thank you again for our quick answere, i will investigate it better. – Labiles Dec 18 '19 at 09:56

0 Answers0