This essay begins with the introduction of cookies, introduces the acquisition and removal of cookie settings, and some small application cases:
First, set a cookie
<script>
Set Cookies:
function Setcookie (name,value,iday) {
if (iday) {
var odate=new Date ();
Odate.setdate (Odate.getdate () +3);
document.cookie=name+ ' = ' +value+ ';p ath=/;expires= ' +odate;
}else{
document.cookie=name+ ' = ' +value+ ';p ath=/; ';
}
}
Setcookie (' Zhangsan ', ' 100 ', 3);
Setcookie (' Lisi ', ' 200 ');
</script>
Second, to obtain a cookie:
<script>
Set Cookies:
function Setcookie (name,value,iday) {
if (iday) {
var odate=new Date ();
Odate.setdate (Odate.getdate () +3);
document.cookie=name+ ' = ' +value+ ';p ath=/;expires= ' +odate;
}else{
document.cookie=name+ ' = ' +value+ ';p ath=/; ';
}
}
Setcookie (' Zhangsan ', ' 100 ', 3);
Setcookie (' Lisi ', ' 200 ');
function GetCookie (name) {
var arr = Document.cookie.split ('; ‘);
for (var i =0; i < arr.length; i++) {
var tmp = arr[i].split (' = ');
if (name = = Tmp[0]) {
return tmp[1];
}
}
Return ';
}
Alert (GetCookie (' Lisi '));
</script>
Iii. Removal of cookies:
<script>
Name:cookie name, Value:cookie value; Iday: Expiry time
Setcookie (Name,value,iday);
function Setcookie (name,value,iday) {
if (iday) {
var odate = new Date ();
Odate.setdate (odate.getdate () + iday);
Document.cookie = name+ ' = ' +value+ ';p ath=/;expires= ' + odate;
}else{
Document.cookie = name+ ' = ' +value+ ';p ath=/';
}
}
Setcookie (' Zhangsan ', ' 100 ', 3);
Setcookie (' Lisi ', ' 200 ');
GetCookie (name);
A=1; Abc=123
function GetCookie (name) {
var arr = Document.cookie.split ('; ‘);
for (var i =0; i < arr.length; i++) {
var tmp = arr[i].split (' = ');
if (name = = Tmp[0]) {
return tmp[1];
}
}
Return ';
}
Removecookie (name);
function Removecookie (name) {
Setcookie (name, ' as ',-1);
}
Removecookie (' Lisi ');
</script>
Four, small case:
1. In the tab, when you leave the page, stay in a module, open again, or the module.
<style>
#box {
width:400px;
height:300px;
border: #000 1px solid;
margin:100px Auto;
}
#box a {
Display:block;
Float:left;
width:100px;
height:39px;
Text-align:center;
line-height:39px;
Background: #ccc;
Color: #333;
Text-decoration:none;
Border-bottom: #333 1px solid;
}
#box a.active{
Background: #c00;
Color: #fff;
width:98px;
Border-left: #333 1px solid;
Border-right: #333 1px solid;
}
#box div{
width:400px;
height:260px;
Text-align:center;
line-height:260px;
font-size:50px;
Display:none;
}
</style>
<script>
function Setcookie (name,value,iday) {
if (iday) {
var odate = new Date ();
Odate.setdate (Odate.getdate () +iday);
Document.cookie = name+ ' = ' +value+ ';p ath=/;expires= ' +odate;
}else{
document.cookie=name+ ' = ' +value+ ';p ath=/';
}
}
function GetCookie (name) {
var arr = Document.cookie.split ('; ‘);
for (var i = 0; i < arr.length; i++) {
var tmp = arr[i].split (' = ');
if (name = = Tmp[0]) {
return tmp[1];
}
}
Return ';
}
Window.onload = function () {
var OBox = document.getElementById (' box ');
var abtn = obox.getelementsbytagname (' a ');
var adiv = obox.getelementsbytagname (' div ');
var index = 0;
var index = GetCookie (' TabIndex ');
if (index) {
tab ();
}
Function tab () {
for (var i = 0; i < abtn.length; i++) {
Abtn[i].classname = ";
Adiv[i].style.display = ' None ';
}
This.classname = ' active ';
Adiv[index].style.display = ' block ';
}
for (var i = 0; i < abtn.length; i++) {
Abtn[i].index = i;
Abtn[i].onclick = function () {
index = This.index;
tab ();
Setcookie (' TabIndex ', this.index,10);
}
}
}
</script>
Cookie II: