Jeevan Mysore
Jeevan Mysore

Reputation: 255

How to Find Cookie Path Using Cookie Name or Getting Cookie Based on Path

How to find out Cookie Path Using Cookie Name? I have a requirement of maintaining a cookie to track exit of an application. I am using window.onbeforeunload event to track exit of a page. Now I have to implement a condition that if referred url is not equal to path of cookie to distinguish between page change within same application to that of application exit. What are the other ways can it be implemented effectively.

Upvotes: 0

Views: 649

Answers (1)

HILARUDEEN S ALLAUDEEN
HILARUDEEN S ALLAUDEEN

Reputation: 1752

You can't do it directly. However you can check where the particular page is having specific cookie or not.

And My Suggestion is create another cookies with path "/"(base path) which will be available in all paths. I mean you have to create separate cookie for every existing cookie and store their paths in newly created cookie. Then only you can track it. This approach also need server side implementation. However, this approach is not recommended(never used) and this will complicate your application architecture.

Here, I assume that If particular cookie path doesn't match with path in address-bar then you want to do some action. In this case,

  1. Simply set cookie to paths whatever you want.
  2. Now page onload event try to fetch require cookie. Then check whether cookie is available or not.
  3. If the cookie is available let the browser do page load or redirect to some other page(or do whatever you want in negative case)

Upvotes: 1

Related Questions