Deleting cookie: how to get its path (and domain)?
up vote
1
down vote
favorite
Various articles and SO answers suggest that deleting cookie via JS should be done like this:
document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
or if we need to specify domain/path, like this:
document.cookie = cookieName + "=" +
(path ? `;path=${path}` : "") +
(domain ? `;domain=${domain}` : "") +
";expires=Thu, 01 Jan 1970 00:00:01 GMT";
Now, in a non-root location (like https://classic.tiddlywiki.com/upgrade/) a more complicated situation takes place. If you open it and type document.cookie in console, you'll get one value (with no domain, path or expires specified):
TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22
and writing in console
document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
doesn't remove the cookie (or change its value) (document.cookie gives the same). Moreover, writing
document.cookie = 'TiddlyWikiClassicOptions=lalala';
doesn't change the existing cookie, but rather addes another one with the same name:
TiddlyWikiClassicOptions=lalala; TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22
That's what I get in clean Chrome. I've also tried that in Vivaldi with installed HTML5 Storage Manager All in One (Chrome extension) and it shows the same, except for the case when 2 cookies are present: while console gives the same line as above (on document.cookie), the extension shows

This doesn't happen at https://classic.tiddlywiki.com/ (deleting/editing works as expected).
Now, I figured that to delete the initial cookie, I had to do
document.cookie = 'TiddlyWikiClassicOptions=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
and if I added the lalala one, I also had to do
document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
to remove the second one.
I wonder, how can I determine which path should I use? document.cookie seems to be silent about the "scope" of each cookie. Or is this something introduced by design and I always have to know which cookie was assigned for which path?
PS I've seen a post which makes me think that there's the same problem about domains (it's in Russian), so I wonder how to get the domain assosiated with the cookie as well.
javascript cookies
add a comment |
up vote
1
down vote
favorite
Various articles and SO answers suggest that deleting cookie via JS should be done like this:
document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
or if we need to specify domain/path, like this:
document.cookie = cookieName + "=" +
(path ? `;path=${path}` : "") +
(domain ? `;domain=${domain}` : "") +
";expires=Thu, 01 Jan 1970 00:00:01 GMT";
Now, in a non-root location (like https://classic.tiddlywiki.com/upgrade/) a more complicated situation takes place. If you open it and type document.cookie in console, you'll get one value (with no domain, path or expires specified):
TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22
and writing in console
document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
doesn't remove the cookie (or change its value) (document.cookie gives the same). Moreover, writing
document.cookie = 'TiddlyWikiClassicOptions=lalala';
doesn't change the existing cookie, but rather addes another one with the same name:
TiddlyWikiClassicOptions=lalala; TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22
That's what I get in clean Chrome. I've also tried that in Vivaldi with installed HTML5 Storage Manager All in One (Chrome extension) and it shows the same, except for the case when 2 cookies are present: while console gives the same line as above (on document.cookie), the extension shows

This doesn't happen at https://classic.tiddlywiki.com/ (deleting/editing works as expected).
Now, I figured that to delete the initial cookie, I had to do
document.cookie = 'TiddlyWikiClassicOptions=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
and if I added the lalala one, I also had to do
document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
to remove the second one.
I wonder, how can I determine which path should I use? document.cookie seems to be silent about the "scope" of each cookie. Or is this something introduced by design and I always have to know which cookie was assigned for which path?
PS I've seen a post which makes me think that there's the same problem about domains (it's in Russian), so I wonder how to get the domain assosiated with the cookie as well.
javascript cookies
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Various articles and SO answers suggest that deleting cookie via JS should be done like this:
document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
or if we need to specify domain/path, like this:
document.cookie = cookieName + "=" +
(path ? `;path=${path}` : "") +
(domain ? `;domain=${domain}` : "") +
";expires=Thu, 01 Jan 1970 00:00:01 GMT";
Now, in a non-root location (like https://classic.tiddlywiki.com/upgrade/) a more complicated situation takes place. If you open it and type document.cookie in console, you'll get one value (with no domain, path or expires specified):
TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22
and writing in console
document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
doesn't remove the cookie (or change its value) (document.cookie gives the same). Moreover, writing
document.cookie = 'TiddlyWikiClassicOptions=lalala';
doesn't change the existing cookie, but rather addes another one with the same name:
TiddlyWikiClassicOptions=lalala; TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22
That's what I get in clean Chrome. I've also tried that in Vivaldi with installed HTML5 Storage Manager All in One (Chrome extension) and it shows the same, except for the case when 2 cookies are present: while console gives the same line as above (on document.cookie), the extension shows

This doesn't happen at https://classic.tiddlywiki.com/ (deleting/editing works as expected).
Now, I figured that to delete the initial cookie, I had to do
document.cookie = 'TiddlyWikiClassicOptions=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
and if I added the lalala one, I also had to do
document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
to remove the second one.
I wonder, how can I determine which path should I use? document.cookie seems to be silent about the "scope" of each cookie. Or is this something introduced by design and I always have to know which cookie was assigned for which path?
PS I've seen a post which makes me think that there's the same problem about domains (it's in Russian), so I wonder how to get the domain assosiated with the cookie as well.
javascript cookies
Various articles and SO answers suggest that deleting cookie via JS should be done like this:
document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
or if we need to specify domain/path, like this:
document.cookie = cookieName + "=" +
(path ? `;path=${path}` : "") +
(domain ? `;domain=${domain}` : "") +
";expires=Thu, 01 Jan 1970 00:00:01 GMT";
Now, in a non-root location (like https://classic.tiddlywiki.com/upgrade/) a more complicated situation takes place. If you open it and type document.cookie in console, you'll get one value (with no domain, path or expires specified):
TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22
and writing in console
document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
doesn't remove the cookie (or change its value) (document.cookie gives the same). Moreover, writing
document.cookie = 'TiddlyWikiClassicOptions=lalala';
doesn't change the existing cookie, but rather addes another one with the same name:
TiddlyWikiClassicOptions=lalala; TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22
That's what I get in clean Chrome. I've also tried that in Vivaldi with installed HTML5 Storage Manager All in One (Chrome extension) and it shows the same, except for the case when 2 cookies are present: while console gives the same line as above (on document.cookie), the extension shows

This doesn't happen at https://classic.tiddlywiki.com/ (deleting/editing works as expected).
Now, I figured that to delete the initial cookie, I had to do
document.cookie = 'TiddlyWikiClassicOptions=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
and if I added the lalala one, I also had to do
document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
to remove the second one.
I wonder, how can I determine which path should I use? document.cookie seems to be silent about the "scope" of each cookie. Or is this something introduced by design and I always have to know which cookie was assigned for which path?
PS I've seen a post which makes me think that there's the same problem about domains (it's in Russian), so I wonder how to get the domain assosiated with the cookie as well.
javascript cookies
javascript cookies
edited Nov 7 at 20:01
asked Nov 7 at 19:37
YakovL
2,751102237
2,751102237
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
If you don't know the (sub)domain and/or the path of the cookie, try expiring all combinations.
var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
var domain = hostname.substr(i+1);
var j = 0;
do {
var path = pathname.substr(0,j+1);
var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
console.log(cookie);
// stacksnippets throws an exception... uncomment to set cookie.
//document.cookie = cookie;
} while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));
This is a reasonable approach that I'll use; I'm not quite happy with the implementation, though (difficult to read and modify), so I've added mine below
– YakovL
2 days ago
Glad this answer was helpful. To each his own in terms of coding style.I'll admit this was a bit of a brain teaser. If you asked 3 developers you'd probably get 4 implementations :) If you really want to get some feedback on your implementation, you could submit it to codereview.stackexchange.com? Cheers.
– dana
2 days ago
add a comment |
up vote
0
down vote
accepted
Here's my implementation of the dana's idea (deleting cookie for each path+domain pair):
function deleteCookieFromAllScopes(cookieName)
{
var domainParts = window.location.hostname.split('.').reverse();
var topLevelDomain = domainParts.shift();
var domains = ;
for(let domainPart of domainParts)
{
let prevDomain = domains.slice(-1)[0] || topLevelDomain;
domains.push(domainPart + '.' + prevDomain);
}
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
for(let domain of domains)
document.cookie = `${cookieName}=; path=${path}; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
It's longer no doubt, but much more readable I think. Suggestions of improvements are welcome.
Here's a modification for a special case of cookies of a page opened via file: scheme:
function deleteCookieFromAllPaths(cookieName)
{
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
document.cookie = `${cookieName}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If you don't know the (sub)domain and/or the path of the cookie, try expiring all combinations.
var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
var domain = hostname.substr(i+1);
var j = 0;
do {
var path = pathname.substr(0,j+1);
var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
console.log(cookie);
// stacksnippets throws an exception... uncomment to set cookie.
//document.cookie = cookie;
} while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));
This is a reasonable approach that I'll use; I'm not quite happy with the implementation, though (difficult to read and modify), so I've added mine below
– YakovL
2 days ago
Glad this answer was helpful. To each his own in terms of coding style.I'll admit this was a bit of a brain teaser. If you asked 3 developers you'd probably get 4 implementations :) If you really want to get some feedback on your implementation, you could submit it to codereview.stackexchange.com? Cheers.
– dana
2 days ago
add a comment |
up vote
1
down vote
If you don't know the (sub)domain and/or the path of the cookie, try expiring all combinations.
var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
var domain = hostname.substr(i+1);
var j = 0;
do {
var path = pathname.substr(0,j+1);
var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
console.log(cookie);
// stacksnippets throws an exception... uncomment to set cookie.
//document.cookie = cookie;
} while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));
This is a reasonable approach that I'll use; I'm not quite happy with the implementation, though (difficult to read and modify), so I've added mine below
– YakovL
2 days ago
Glad this answer was helpful. To each his own in terms of coding style.I'll admit this was a bit of a brain teaser. If you asked 3 developers you'd probably get 4 implementations :) If you really want to get some feedback on your implementation, you could submit it to codereview.stackexchange.com? Cheers.
– dana
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
If you don't know the (sub)domain and/or the path of the cookie, try expiring all combinations.
var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
var domain = hostname.substr(i+1);
var j = 0;
do {
var path = pathname.substr(0,j+1);
var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
console.log(cookie);
// stacksnippets throws an exception... uncomment to set cookie.
//document.cookie = cookie;
} while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));If you don't know the (sub)domain and/or the path of the cookie, try expiring all combinations.
var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
var domain = hostname.substr(i+1);
var j = 0;
do {
var path = pathname.substr(0,j+1);
var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
console.log(cookie);
// stacksnippets throws an exception... uncomment to set cookie.
//document.cookie = cookie;
} while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
var domain = hostname.substr(i+1);
var j = 0;
do {
var path = pathname.substr(0,j+1);
var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
console.log(cookie);
// stacksnippets throws an exception... uncomment to set cookie.
//document.cookie = cookie;
} while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
var domain = hostname.substr(i+1);
var j = 0;
do {
var path = pathname.substr(0,j+1);
var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
console.log(cookie);
// stacksnippets throws an exception... uncomment to set cookie.
//document.cookie = cookie;
} while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));edited Nov 7 at 22:29
answered Nov 7 at 20:05
dana
12.2k24068
12.2k24068
This is a reasonable approach that I'll use; I'm not quite happy with the implementation, though (difficult to read and modify), so I've added mine below
– YakovL
2 days ago
Glad this answer was helpful. To each his own in terms of coding style.I'll admit this was a bit of a brain teaser. If you asked 3 developers you'd probably get 4 implementations :) If you really want to get some feedback on your implementation, you could submit it to codereview.stackexchange.com? Cheers.
– dana
2 days ago
add a comment |
This is a reasonable approach that I'll use; I'm not quite happy with the implementation, though (difficult to read and modify), so I've added mine below
– YakovL
2 days ago
Glad this answer was helpful. To each his own in terms of coding style.I'll admit this was a bit of a brain teaser. If you asked 3 developers you'd probably get 4 implementations :) If you really want to get some feedback on your implementation, you could submit it to codereview.stackexchange.com? Cheers.
– dana
2 days ago
This is a reasonable approach that I'll use; I'm not quite happy with the implementation, though (difficult to read and modify), so I've added mine below
– YakovL
2 days ago
This is a reasonable approach that I'll use; I'm not quite happy with the implementation, though (difficult to read and modify), so I've added mine below
– YakovL
2 days ago
Glad this answer was helpful. To each his own in terms of coding style.I'll admit this was a bit of a brain teaser. If you asked 3 developers you'd probably get 4 implementations :) If you really want to get some feedback on your implementation, you could submit it to codereview.stackexchange.com? Cheers.
– dana
2 days ago
Glad this answer was helpful. To each his own in terms of coding style.I'll admit this was a bit of a brain teaser. If you asked 3 developers you'd probably get 4 implementations :) If you really want to get some feedback on your implementation, you could submit it to codereview.stackexchange.com? Cheers.
– dana
2 days ago
add a comment |
up vote
0
down vote
accepted
Here's my implementation of the dana's idea (deleting cookie for each path+domain pair):
function deleteCookieFromAllScopes(cookieName)
{
var domainParts = window.location.hostname.split('.').reverse();
var topLevelDomain = domainParts.shift();
var domains = ;
for(let domainPart of domainParts)
{
let prevDomain = domains.slice(-1)[0] || topLevelDomain;
domains.push(domainPart + '.' + prevDomain);
}
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
for(let domain of domains)
document.cookie = `${cookieName}=; path=${path}; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
It's longer no doubt, but much more readable I think. Suggestions of improvements are welcome.
Here's a modification for a special case of cookies of a page opened via file: scheme:
function deleteCookieFromAllPaths(cookieName)
{
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
document.cookie = `${cookieName}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
add a comment |
up vote
0
down vote
accepted
Here's my implementation of the dana's idea (deleting cookie for each path+domain pair):
function deleteCookieFromAllScopes(cookieName)
{
var domainParts = window.location.hostname.split('.').reverse();
var topLevelDomain = domainParts.shift();
var domains = ;
for(let domainPart of domainParts)
{
let prevDomain = domains.slice(-1)[0] || topLevelDomain;
domains.push(domainPart + '.' + prevDomain);
}
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
for(let domain of domains)
document.cookie = `${cookieName}=; path=${path}; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
It's longer no doubt, but much more readable I think. Suggestions of improvements are welcome.
Here's a modification for a special case of cookies of a page opened via file: scheme:
function deleteCookieFromAllPaths(cookieName)
{
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
document.cookie = `${cookieName}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Here's my implementation of the dana's idea (deleting cookie for each path+domain pair):
function deleteCookieFromAllScopes(cookieName)
{
var domainParts = window.location.hostname.split('.').reverse();
var topLevelDomain = domainParts.shift();
var domains = ;
for(let domainPart of domainParts)
{
let prevDomain = domains.slice(-1)[0] || topLevelDomain;
domains.push(domainPart + '.' + prevDomain);
}
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
for(let domain of domains)
document.cookie = `${cookieName}=; path=${path}; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
It's longer no doubt, but much more readable I think. Suggestions of improvements are welcome.
Here's a modification for a special case of cookies of a page opened via file: scheme:
function deleteCookieFromAllPaths(cookieName)
{
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
document.cookie = `${cookieName}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
Here's my implementation of the dana's idea (deleting cookie for each path+domain pair):
function deleteCookieFromAllScopes(cookieName)
{
var domainParts = window.location.hostname.split('.').reverse();
var topLevelDomain = domainParts.shift();
var domains = ;
for(let domainPart of domainParts)
{
let prevDomain = domains.slice(-1)[0] || topLevelDomain;
domains.push(domainPart + '.' + prevDomain);
}
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
for(let domain of domains)
document.cookie = `${cookieName}=; path=${path}; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
It's longer no doubt, but much more readable I think. Suggestions of improvements are welcome.
Here's a modification for a special case of cookies of a page opened via file: scheme:
function deleteCookieFromAllPaths(cookieName)
{
var path = window.location.pathname;
var paths = ['/'], pathLength = 1, nextSlashPosition;
while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
{
pathLength = nextSlashPosition + 1;
paths.push(path.substr(0, pathLength));
}
for(let path of paths)
document.cookie = `${cookieName}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
edited yesterday
answered 2 days ago
YakovL
2,751102237
2,751102237
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196593%2fdeleting-cookie-how-to-get-its-path-and-domain%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password