且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Cookie只能通过特定的URL访问

更新时间:2023-12-03 08:27:58

这是您需要做的:

假设您在页面上,网址为 http://www.example.com/pages/myPage.html ,并且仅在设置/创建您拥有的Cookie时,您才想限制对此特定页面( myPage.html )上Cookie的访问设置路径参数当前页面相对路径.

Suppose you are on the page with url http://www.example.com/pages/myPage.html, and you want to limit the access to cookies on this particular page (myPage.html) only then while setting/creating the cookie you have the set the path parameter as current page relative path.

var pathToMyPage = window.location.pathname; // this gives pages/myPage.html
document.cookie('name=value;path='+ pathToMyPage); 

现在,如果您尝试在其他页面的cookie中查找此特定键,请说/pages/myPage2.html .您不会在那找到它.

Now if you try to look for this particular key in the cookie of other pages say /pages/myPage2.html. You won't find it there.