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

javascriptweb developmentfront end technology

要为多个页面设置 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>

相关文章


有用资源