There are many ways to set up a cookie in JS.
The first type: (This is the Code of the website)
Copy Code
<script>
Set cookies
function Setcookie (CNAME, cvalue, exdays) {
var d = new Date ();
D.settime (D.gettime () + (exdays*24*60*60*1000));
var expires = "expires=" +d.toutcstring ();
Document.cookie = cname + "=" + Cvalue + ";" + expires;
}
Get cookies
function GetCookie (CNAME) {
var name = cname + "=";
var ca = Document.cookie.split (';');
for (var i=0; i<ca.length; i++) {
var c = Ca[i];
while (C.charat (0) = = ") c = c.substring (1);
if (C.indexof (name)! =-1) return c.substring (Name.length, c.length);
}
Return "";
}
Clearing cookies
function ClearCookie (name) {
Setcookie (Name, "",-1);
}
function Checkcookie () {
var user = GetCookie ("username");
if (User! = "") {
Alert ("Welcome again" + user);
} else {
user = prompt ("Please enter your name:", "");
if (User! = "" && user! = null) {
Setcookie ("username", user, 365);
}
}
}
Checkcookie ();
</script>
Copy Code
The second type:
Copy Code
<script>
JS Operation Cookies Method!
Write cookies
function Setcookie (c_name, value, Expiredays) {
var exdate=new Date ();
Exdate.setdate (exdate.getdate () + expiredays);
Document.cookie=c_name+ "=" + Escape (value) + ((expiredays==null)? "": "; expires=" +exdate.togmtstring ());
}
Read cookies
function GetCookie (name)
{
var arr,reg=new RegExp ("(^|)" +name+ "= ([^;] *)(;|$)");
if (Arr=document.cookie.match (REG))
Return (arr[2]);
Else
return null;
}
Delete Cookies
function Delcookie (name)
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval=getcookie (name);
if (cval!=null)
document.cookie= name + "=" +cval+ "; expires=" +exp.togmtstring ();
}
Using the example
Setcookie (' username ', ' Darren ', 30)
Alert (GetCookie ("username"));
</script>
Copy Code
A third example
Copy Code
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script language= "JavaScript" type= "Text/javascript" >
function Addcookie (objname, ObjValue, objhours) {//Add cookie
var str = objname + "=" + Escape (ObjValue);
if (objhours > 0) {//= 0 does not set expiration time, the cookie disappears automatically when the browser shuts down
var date = new Date ();
var ms = Objhours * 3600 * 1000;
Date.settime (Date.gettime () + ms);
str + = "; Expires= "+ date.togmtstring ();
}
Document.cookie = str;
Alert ("Add cookie Success");
}
function GetCookie (objname) {//Gets the value of the cookie for the specified name
var arrstr = Document.cookie.split (";");
for (var i = 0; i < arrstr.length; i++) {
var temp = arrstr[i].split ("=");
if (temp[0] = = objname)
Return unescape (temp[1]);
}
}
function Delcookie (name) {//In order to delete a cookie of the specified name, you can set its expiration time to a past time
var date = new Date ();
Date.settime (Date.gettime ()-10000);
Document.cookie = name + "=A; Expires= "+ date.togmtstring ();
}
function Allcookie () {//Read all saved cookie strings
var str = document.cookie;
if (str = = "") {
str = "No cookie Saved";
}
alert (str);
}
function $ (m, N) {
return document.forms[m].elements[n].value;
}
function add_ () {
var cookie_name = $ ("MyForm", "cookie_name");
var Cookie_value = $ ("MyForm", "Cookie_value");
var cookie_expirehours = $ ("MyForm", "cookie_expireshours");
Addcookie (Cookie_name, Cookie_value, cookie_expirehours);
}
function get_ () {
var cookie_name = $ ("MyForm", "cookie_name");
var cookie_value = GetCookie (cookie_name);
alert (Cookie_value);
}
function Del_ () {
var cookie_name = $ ("MyForm", "cookie_name");
Delcookie (Cookie_name);
Alert ("Delete succeeded");
}
</script>
<body>
<form name= "MyForm" >
<div>
<label for= "Cookie_name" >
Name
</label>
<input type= "text" name= "Cookie_name"/>
</div>
<div>
<label for= "Cookie_value" >
Value
</lable>
<input type= "text" name= "Cookie_value"/>
</div>
<div>
<label for= "Cookie_expirehours" >
How many hours are out of date
</lable>
<input type= "text" name= "Cookie_expireshours"/>
</div>
<div>
<input type= "button" value= "Add this cookie" onclick= "add_ ()"/><input type= "button" value= "Read All Cookies" onclick= " Allcookie () "/><input type=" button "value=" reads the name cookie "onclick=" get_ () "/><input type=" button "value=" Delete the name cookie "onclick=" del_ () "/>
</div>
</form>
</body>
Copy Code
Attention:
The Chrome browser does not get cookies locally. Must be available on the server. If it is local, you can put it under the WWW directory.
Google Chrome only supports the reading and writing of cookies on online websites, which is forbidden for local HTML cookie operations. So the following code if you write in a local HTML file, will pop up the dialog box content is empty.
Document.cookie = "Test=cooo";
alert (Document.cookie);
If this page is the content of the online website, the cookie content test=cooo and so on will be displayed normally.
JS setting cookies, deleting cookies