-
Is there a way I can enable cookies to exist in specific paths only? Meaning that if I want a cookie on a certain webpage e.g. 'hasUserVisited' and the value being 'abcdefg', to exist on path '/site/abcdefg', I also want one on '/site/hijklmno' with the same cookie name of 'hasUserVisited' and value of 'hijklmno'. So if I go to a new webpage/path where the user has not visited, the value of 'hasUserVisited' should return null. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You should be able to utilize the if (Cookies.get('hasUserVisited')) {
// User was here before…
} else {
var currentPath = window.location.path
Cookies.set('hasUserVisited', true, { path: currentPath })
} There‘s one caveat though, this won’t work well within a hierarchy of pages, because a cookie at path If all you want to do is track whether a visitor has visited particular sites and don‘t need to transmit this information back to the server (that‘s what cookies are for) you should probably be using |
Beta Was this translation helpful? Give feedback.
You should be able to utilize the
path
cookie attribute for this. E.g.There‘s one caveat though, this won’t work well within a hierarchy of pages, because a cookie at path
/parent
will also be available at path/parent/child
and there is no way to get a cookie by path.If all you want to do is track whether a visitor has visited particular sites and don‘t need to transmit this information back to the server (that‘s what cookies are for) you should probably be using
localStorage
for the job. It would let you store …