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