How to set named cookies in JavaScript?

To set named cookie in JavaScript, run the following code. It sets a customer name in an input cookie.

Example

<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>
Updated on: 2019-10-03T07:29:46+05:30

629 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements