如何使用 JavaScript 为多页设定 Cookie?


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

;path=/

示例

上述操作会将您的 cookie 设置为 site 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-6-2020

1K+ 浏览

开启您的职业生涯

完成课程以获得认证

立即开始
广告
© . All rights reserved.