javascript实现用户登录保存帐号密码(保存在cookies中)
/*这个是html的名字123.html*/<SCRIPT language=javascript>
function setCookies(name,value)
{
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape(value) +";expire*="+ **p.toGMTString();
}
function getCookies(name)
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}
function getcookies()
{
document.form1.UserName.value=getCookies("Loginusername");
\把你要表单初始化放在这
}
function delCookies(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie=name +"="+cval+";expire*="+**p.toGMTString();
}
function submit()
{
username=document.form1.UserName.value;
setCookies('Loginusername',username)
\创建一个cookies,第一个为名字,后面的为值
}
</script>
<body onload="getcookies()">
</body>
<script type="text/javascript" src="123.js"></script>
<script type="text/javascript">
function foo()
{
var name = document.getElementById("name").value;
if(name)
{
setCookie("name",name);
}
document.getElementById("show").innerHTML+=("NEWEST NAME :"+ getCookie("name") + "<br>");
}
</script>
Chapter 1.9 example
<br>
<input type="text" id="name" size="20" />
<input type="button" value="setCookie" onclick="foo()" />
<div id="show"></div>
/////////////////////////////////////////////////////示例源码
<!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>js操作cookie 易网时代网络技术服务中心 http://www.escdns.com%3c/title>
</head>
<body>
<input type="button" name="button" id="button" value="Remove Cookies" onclick="window.cookie.remove( )" />
<script language="javascript" type="text/javascript">
function Cookie() {
var self = this;
var trim = function(str){
return str.replace(/(^s*)|(s*$)/g, "");
}
var init = function(){
var allcookies = document.cookie;
if (allcookies == "") return;
var cookies = allcookies.split(';');
for(var i=0; i < cookies.length; i++) // Break each pair into an array
cookies[i] = cookies[i].split('=');
for(var i = 0; i < cookies.length; i++) {
self[trim(cookies[i][0])] = decodeURIComponent(cookies[i][1]);
}
}
init();
this.save = function(daysToLive, path, domain, secure){
var dt = (new Date()).getTime() + daysToLive * 24 * 60 * 60 * 1000;
for(var prop in this) {
if (typeof this[prop] == 'function')
continue;
var cookie = "";
cookie = prop + '=' + encodeURIComponent(this[prop]);
if (daysToLive || daysToLive == 0) cookie += ";expires=" + new Date(dt).toUTCString();
if (path) cookie += ";path=" + path;
if (domain) cookie += "; domain=" + domain;
if (secure) cookie += ";secure";
document.cookie = cookie;
}
}
this.remove = function(path, domain, secure){
self.save(0, path, domai