A small demo of JavaScript reading and writing cookies
Last Update:2017-02-28
Source: Internet
Author: User
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>
<H1 align= "center" >cookie Read and write test <form id= "Login" name= "Login" >
<table width= "100%" >
<tr>
<TD align= "right" >user name:</td>
<td><input type= "text" id= "username"/></td>
</tr>
<tr>
<TD align= "right" >Password:</td>
<td><input type= "password" id= "password"/></td>
</tr>
<TR><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>