How to use JavaScript to set cookies for multiple pages?

To set cookies for multiple pages, you need to set the path as −



The above will make your cookies to site cookie. You can try to run the following code to set cookies for multiple pages −

Live Demo

            function WriteCookie() {
               if( document.myform.customer.value == "" ) {
                  alert("Enter some value!");
               cookievalue = escape(document.myform.customer.value) + ";";
               document.cookie = "name=" + cookievalue + "; path=/";
               document.write ("Setting Cookies : " + "name=" + cookievalue );
      <form name = "myform" action = "">
         Enter name: <input type="text" name="customer"/>
         <input type="button" value="Set Cookie" onclick="WriteCookie();"/>

Updated on: 16-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started