Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีใช้ JavaScript ตั้งค่า Cookies สำหรับหน้าแรกเท่านั้น


เพื่อตั้งค่า cookies สำหรับหน้าแรก ให้เพิ่มหน้านั้นไปยังหน้าแรกเอง

ตัวอย่าง

คุณสามารถลองรันโค้ดต่อไปนี้เพื่อตั้งค่า 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>