Access cookie of different, more specific path, but same domain?

is it possible to access cookies set on a different path (but same domain) with js? so say the web page with js being accessed is a file in the root html directory, and for that domain a cookie is already set in the browser with a path say, ‘/abc/’. is it possible to access that cookie from the just described js?

thanks.

Yes it is possible by using the path attribute. See the document.cookie documentation for details, and [url=“http://www.quirksmode.org/js/cookies.html”]quirksmode cookies for some good functions to work with cookies.

thanks for the answer.

i’ve got a webpage in the html root folder with this js:
document.write( document.cookie );

there’s an image on the page whose directory is /abc/ and that image is setting a cookie for that path (using php).

the js, even on repeat visits, outputs nothing. but changing the path the cookie is set for from ‘/abc/’ to ‘/’ in the php image, on first visit the js still outputs nothing, on visits after that it outputs the cookie key and value as set by php.

i don’t see how to access the cookie when it’s set for a different path? do i need to do something different to document.write( document.cookie ); to be able to see the cookie set in a different path?

thanks.

JavaScript only reads cookies from the current path, even though it can set them for different paths.

Using server-side code though you can access the cookie from different paths. You might use ajax to run a server-side script at a certain path, so that the response is then fed back to you.

Perhaps will help you more is to explain the why you want to do this. There may be other pre-built solutions that already exist that can help you.

> JavaScript only reads cookies from the current path, even though it can set them for different paths.

right, i see.

> Perhaps will help you more is to explain the why you want to do this.

a homemade tracking/analytics thing. i’m making it up as i go along so not so easy to explain, plus, this

> You might use ajax to run a server-side script at a certain path, so that the response is then fed back to you.

completely solves it for me i think, so that’s great, thanks.