如何使用 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.