各种文章和SO answer都建议通过JS删除cookie的方法如下:

document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';


或者,如果我们需要指定域/路径,例如:

document.cookie = cookieName + "=" +
    (path   ? `;path=${path}` : "") +
    (domain ? `;domain=${domain}` : "") +
    ";expires=Thu, 01 Jan 1970 00:00:01 GMT";


现在,在非根目录(如https://classic.tiddlywiki.com/upgrade/)中,情况更加复杂。如果打开它并在控制台中键入document.cookie,将得到一个值(未指定domainpathexpires):

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


并在控制台中编写

document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';


不会删除cookie(或更改其值)(document.cookie表示相同)。而且,写作

document.cookie = 'TiddlyWikiClassicOptions=lalala';


不会更改现有的cookie,而是添加另一个同名的cookie:

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


这就是我在干净的Chrome中获得的东西。我还尝试在安装了HTML5 Storage Manager All in One(Chrome扩展程序)的Vivaldi中显示相同的内容,但存在2个cookie的情况除外:而控制台给出的行与上述相同(在document.cookie上) ,扩展名显示

javascript - 删除Cookie:如何获取其路径(和域)?-LMLPHP

https://classic.tiddlywiki.com/不会发生这种情况(删除/编辑工作按预期进行)。

现在,我发现要删除初始Cookie,我必须

document.cookie = 'TiddlyWikiClassicOptions=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';


如果我添加lalala一个,我也必须做

document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';


删除第二个。

我想知道如何确定应该使用哪个路径? document.cookie似乎对每个cookie的“范围”保持沉默。还是这是设计引入的,我总是必须知道为哪个路径分配了哪个cookie?

PS:我看过一篇帖子,使我认为域(it's in Russian)也存在相同的问题,所以我想知道如何将域与cookie关联。

最佳答案

如果您不知道Cookie的(子)域和/或路径,请尝试使所有组合过期。



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('.'));

关于javascript - 删除Cookie:如何获取其路径(和域)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53196593/

10-12 02:14