如何使用 JavaScript 为多个页面设置 cookie?


要为多个页面设置 cookie,你需要将路径设置为 −

;path=/

示例

上述内容会将你的 cookie 设置为站点 cookie。你可以尝试运行以下代码,为多个页面设置 cookie −

动态演示

<html>
   <head>
      <script>
         <!--
            function WriteCookie() {
               if( document.myform.customer.value == "" ) {
                  alert("Enter some value!");
                  return;
               }
               cookievalue = escape(document.myform.customer.value) + ";";
               document.cookie = "name=" + cookievalue + "; path=/";
               document.write ("Setting Cookies : " + "name=" + cookievalue );
            }
         //-->
      </script>
   </head>
   <body>
      <form name = "myform" action = "">
         Enter name: <input type="text" name="customer"/>
         <input type="button" value="Set Cookie" onclick="WriteCookie();"/>
      </form>
   </body>
</html>

更新于: 16-Jun-2020

1000+ 次浏览

开启您的 职业生涯

通过完成该课程获得认证

开始
Advertisement
© . All rights reserved.