Cookie II:

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.