How to get a specified Cookie path using javascript

大城市里の小女人 提交于 2020-01-05 05:37:25

问题


Is there anyway to get a specified cookie's Path in Javascript ,

i have a javascript method which will return cookie value based on the name . Is it possible to add filter to the method such that it will fetch from particular paths cookies only ?

requirement is to fetch cookie from a path named /ctp ?

function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; }


回答1:


I think you're looking for cookies.getAll(), which requires setting up a manifest.webmanifest file and there's caveats among support issues.

Personally I think localStorage and/or sessionStorage are a far better choice... well if keeping one's sanity is an option, otherwise best of luck keeping values fresh across objects reading and writing data.



来源:https://stackoverflow.com/questions/38026119/how-to-get-a-specified-cookie-path-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!