根据路径和查询字符串

时间:2015-08-11 09:11:15

标签: javascript cookies query-string

我想为网站上的每个页面存储不同的cookie。如果用户在该页面上采取了特定操作,cookie将记录。

这很好,页面有一个直接的URL。我可以像这样存储cookie:

// https://www.example.com/path/to/page.html
document.cookie = "name=" + value + ";max-age=2419200;path=" + location.pathname;

但是如果页面的URL使用查询字符串来显示不同的"页面",则会失败。我没有运气就试过以下内容(即cookie甚至没有存储在Chrome中):

// https://www.example.com/path/to/page.html?story=10
document.cookie = "name=" + value + ";max-age=2419200;path=" + location.pathname + location.search;

The docs仅包含pathdomain的参数。是否可以根据查询字符串存储cookie?也许不是。那么有一个很好用的解决方法吗?

0 个答案:

没有答案
相关问题