A small demo of JavaScript reading and writing cookies

Source: Internet
Author: User
Tags date split
Cookie|javascript <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" " HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">
<title>test cookie</title>
<link rel= "Stylesheet" type= text/css "href=". /css/mainstylesheet.css "/>
<body>
&LT;H1 align= "center" >cookie Read and write test <form id= "Login" name= "Login" >
<table width= "100%" >
<tr>
&LT;TD align= "right" >user name:</td>
<td><input type= "text" id= "username"/></td>
</tr>
<tr>
&LT;TD align= "right" >Password:</td>
<td><input type= "password" id= "password"/></td>
</tr>
&LT;TR&GT;&LT;TD colspan= "2" align= "Center" >
<input type= "Submit" id= "OK" value= "writer"/>
</td></tr>
</table>
</form>
<script language= "javascript" type= "Text/javascript" >
<!--
function writecookie (FM)
{
var cookiename = Fm.username.value;
var cookievalue = Fm.password.value;
var codecookievalue = "";
for (var i = 0;i < cookievalue.length; ++i)
{
Codecookievalue + + cookievalue.charcodeat (i);
if (I < cookievalue.length-1)
Codecookievalue + = "+";
}
var cookie = cookiename + "=" + Codecookievalue;
var date = new Date ();
Date.settime (Date.gettime () + 100000);
var expirestr = date.togmtstring ();
Cookies + + "; expires= "+ expirestr;
Document.cookie = cookie;
}
-->
</script>
<input id= "read" type= "button" value= "Read Cookie"/>
<script language= "javascript" type= "Text/javascript" >
<!--
function Readcookie ()
{
var Cookiepair = document.cookie.split ("=");
if (cookiepair[0]!= null
&& cookiepair[1]!= null)
{
var truevalue = "";
var arraycode = cookiepair[1].split ("+");
for (var i = 0; i < arraycode.length; ++i)
TrueValue + + string.fromcharcode (Arraycode[i]);
if (Cookiepair!= null)
{
Document.writeln ("<table width=\" 100%\ ">");
Document.writeln ("<tr>");
Document.writeln ("<td>");
Document.writeln ("User name:");
Document.writeln ("<\/td>");
Document.writeln ("<td>");
Document.writeln (Cookiepair[0]);
Document.writeln ("<\/td>");
Document.writeln ("<\/tr>");
Document.writeln ("<tr>");
Document.writeln ("<td>");
Document.writeln ("Password:");
Document.writeln ("<\/td>");
Document.writeln ("<td>");
Document.writeln (TrueValue);
Document.writeln ("<\/td>");
Document.writeln ("<\/tr>");
Document.writeln ("<\/table>");
}
}
Else
Document.writeln ("no cookie!");
}
-->
</script>
</body>

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.