How to use JavaScript to set cookies for homepage only?

JavascriptWeb DevelopmentFront End Technology

To set cookies for a homepage, add the page to the homepage itself.

Example

You can try to run the following code to set cookies

Live Demo

<html>
   <head>
      <script>
         <!--
            function WriteCookie() {
               if( document.myform.customer.value == "" ){
                  alert("Enter some value!");
                  return;
               }
               cookievalue= escape(document.myform.customer.value) + ";";

               var myPath = window.location.pathname;
               var address = query.split("/");

               if ((address[address.length - 1]).toLowerCase() == "home.html") {
                  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>
raja
Published on 31-Jan-2018 14:24:33
Advertisements