Skip to content

Cookies that exist in webpages only #726

Answered by carhartl
praj18 asked this question in Q&A
Discussion options

You must be logged in to vote

You should be able to utilize the path cookie attribute for this. E.g.

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 /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 …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@praj18
Comment options

Answer selected by praj18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants