Cookie read/write operations

Source: Internet
Author: User

Write the content to the cookie on one page, and obtain and display the content on the other page in the page (either in safari or firefox, but not in chrome, I don't know why .)
Page 1: click the button to write the form content to the cookie,
[Html]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> setCookie </title>
 
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> test2 </title>
<Script type = "text/javascript">
 
// Write cookie
Function writeCookie (name, value, hours ){
Var expire = "";
If (hours! = Null ){
Expire = new Date (). getTime () + hours * 3600000 );
Expire = "; expires =" + expire. toGMTString ();
}
Document. cookie = name + "=" + escape (value) + expire;
}

// Obtain the cookie
Function getCookie (Name)
{
Var search = Name + "= ";

If (document. cookie. length> 0)
{

Offset = document. cookie. indexOf (search)
If (offset! =-1)
{
Offset + = search. length
End = document. cookie. indexOf (";", offset)
If (end =-1) end = document. cookie. length
Return unescape (document. cookie. substring (offset, end ))
}
Else return "";
}
}
// Click the image to obtain the cookie.
Function getName (){
Var button = document. getElementById ("button ");
Var inhtml = document. getElementById ("inhtml ");
Button. onclick = function (){

Var name = getCookie ("username ");

Inhtml. innerHTML = name;
}

}
// Click Submit to write the cookie
Function add (){
Var connect = document. getElementById ("connect ");
Alert (connect. value );

WriteCookie ("username", connect. value, 24 );
}
 
</Script>
</Head>
 
<Body onload = "getName ();">
 
</Body>
 
 
<Form onsubmit = "add ();">
<Input type = "text" id = "connect"/>
<Input type = "submit" value = "submit"/>
</Form>
 
<Span id = "inhtml"> </span>
 
</Head>
 
<Body>
</Body>
</Html>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> setCookie </title>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> test2 </title>
<Script type = "text/javascript">

// Write cookie
Function writeCookie (name, value, hours ){
Var expire = "";
If (hours! = Null ){
Expire = new Date (). getTime () + hours * 3600000 );
Expire = "; expires =" + expire. toGMTString ();
}
Document. cookie = name + "=" + escape (value) + expire;
}

// Obtain the cookie
Function getCookie (Name)
{
Var search = Name + "= ";

If (document. cookie. length> 0)
{

Offset = document. cookie. indexOf (search)
If (offset! =-1)
{
Offset + = search. length
End = document. cookie. indexOf (";", offset)
If (end =-1) end = document. cookie. length
Return unescape (document. cookie. substring (offset, end ))
}
Else return "";
}
}
// Click the image to obtain the cookie.
Function getName (){
Var button = document. getElementById ("button ");
Var inhtml = document. getElementById ("inhtml ");
Button. onclick = function (){

Var name = getCookie ("username ");

Inhtml. innerHTML = name;
}

}
// Click Submit to write the cookie
Function add (){
Var connect = document. getElementById ("connect ");
Alert (connect. value );

WriteCookie ("username", connect. value, 24 );
}

</Script>
</Head>

<Body onload = "getName ();">

</Body>


<Form onsubmit = "add ();">
<Input type = "text" id = "connect"/>
<Input type = "submit" value = "submit"/>
</Form>

<Span id = "inhtml"> </span>

</Head>

<Body>
</Body>
</Html>


Click the image on the second page to display the cookie;
[Html]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> cookietest3 </title>
 
 
<Script type = "text/javascript">
 


// Obtain the cookie
Function getCookie (Name)
{
Var search = Name + "= ";

If (document. cookie. length> 0)
{

Offset = document. cookie. indexOf (search)
If (offset! =-1)
{
Offset + = search. length
End = document. cookie. indexOf (";", offset)
If (end =-1) end = document. cookie. length
Return unescape (document. cookie. substring (offset, end ))
}
Else return "";
}
}
// Click the image to obtain the cookie.
Function getName (){
Var button = document. getElementById ("button ");
Var inhtml = document. getElementById ("inhtml ");
Button. onclick = function (){

Var name = getCookie ("username ");

Inhtml. innerHTML = name;
}

}

</Script>
</Head>
 
<Body onload = "getName ();">
 
 

<Span id = "inhtml"> </span>
</Body>
</Html>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> cookietest3 </title>


<Script type = "text/javascript">



// Obtain the cookie
Function getCookie (Name)
{
Var search = Name + "= ";

If (document. cookie. length> 0)
{

Offset = document. cookie. indexOf (search)
If (offset! =-1)
{
Offset + = search. length
End = document. cookie. indexOf (";", offset)
If (end =-1) end = document. cookie. length
Return unescape (document. cookie. substring (offset, end ))
}
Else return "";
}
}
// Click the image to obtain the cookie.
Function getName (){
Var button = document. getElementById ("button ");
Var inhtml = document. getElementById ("inhtml ");
Button. onclick = function (){

Var name = getCookie ("username ");

Inhtml. innerHTML = name;
}

}
 
</Script>
</Head>

<Body onload = "getName ();">



<Span id = "inhtml"> </span>
</Body>
</Html>


 

Related Article

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.