如何在 JavaScript 中設定命名 cookie?


要在 JavaScript 中設定命名 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;
               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>

更新於: 03-Oct-2019

500 次觀看

開啟你的 職業生涯

獲得認證,完成課程

開始
提示