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

开启你的 职业生涯

完成课程认证

开始
广告
© . All rights reserved.