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

更新於:2019 年 10 月 3 日

500 次瀏覽

開啟你的 職業生涯

完成課程認證

開始
廣告