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

วิธีสร้าง Domain-based Cookies ด้วย JavaScript


เพื่อสร้าง domain-based cookie ให้ตั้งค่า domain และ path attribute บน cookie ของคุณ เช่น:

domain=.example.com

ตัวอย่าง

คุณสามารถลองรันโค้ดต่อไปนี้เพื่อเรียนรู้การสร้าง domain-based cookies:

Live Demo

<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 + ";                domain=.example.com;path=/";                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>