Code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>JS操作Cookie</title>
<script language="javascript" type="text/javascript">
var COOKIE_NAME="USERNAME"
/*
功能:保存cookies函数
参数:name,cookie名字;value,值
*/
function SetCookie(name,value){
var Days = 7; //cookie 将被保存两个月
var exp = new Date(); //获得当前时间
exp.setTime(exp.getTime() + Days*24*60*60*1000); //换成毫秒
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
/*
功能:获取cookies函数
参数:name,cookie名字
*/
function GetCookie(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null)
return unescape(arr[2]);
return null;
}
/*
功能:删除cookies函数
参数:name,cookie名字
*/
function DelCookie(name){
var exp = new Date(); //当前时间
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
/*
//取文档指定Id值
*/
function isDefined(type) {
return (type != 'undefined' && type != 'unknown');
}
function childNodes(element) {
if (isDefined(typeof element.childNodes)) {
return element.childNodes;
} else if (isDefined(typeof element.children)) {
return element.children;
}
}
function getElementById(element_name) {
if (isDefined(typeof document.getElementById)) {
return document.getElementById(element_name);
} else if(typeof isDefined(document.all)) {
return document.all[element_name];
}
}
function setTextContent(elem, content) {
if (isDefined(typeof elem.innerText)) {
elem.innerText = content;
} else if (isDefined(typeof elem.textContent)) {
elem.textContent = content;
}
}
//=============================================
function TestLogin()
{
delCookie(COOKIE_NAME);
var username=getElementById('txtUserName').value;
SetCookie(COOKIE_NAME,username);
//var cookievalue=GetCookie(COOKIE_NAME);
window.location.href="9.htm?u="+username;
//alert(cookievalue);
}
</script>
</head>
<body>
<input id="txtUserName" type="text" /><input id="Button1" type="button" value="login" onclick='TestLogin();' />
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>JS操作Cookie</title>
<script language="javascript" type="text/javascript">
var COOKIE_NAME="USERNAME"
/*
功能:保存cookies函数
参数:name,cookie名字;value,值
*/
function SetCookie(name,value){
var Days = 7; //cookie 将被保存两个月
var exp = new Date(); //获得当前时间
exp.setTime(exp.getTime() + Days*24*60*60*1000); //换成毫秒
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
/*
功能:获取cookies函数
参数:name,cookie名字
*/
function GetCookie(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null)
return unescape(arr[2]);
return null;
}
/*
功能:删除cookies函数
参数:name,cookie名字
*/
function DelCookie(name){
var exp = new Date(); //当前时间
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
/*
//取文档指定Id值
*/
function isDefined(type) {
return (type != 'undefined' && type != 'unknown');
}
function childNodes(element) {
if (isDefined(typeof element.childNodes)) {
return element.childNodes;
} else if (isDefined(typeof element.children)) {
return element.children;
}
}
function getElementById(element_name) {
if (isDefined(typeof document.getElementById)) {
return document.getElementById(element_name);
} else if(typeof isDefined(document.all)) {
return document.all[element_name];
}
}
function setTextContent(elem, content) {
if (isDefined(typeof elem.innerText)) {
elem.innerText = content;
} else if (isDefined(typeof elem.textContent)) {
elem.textContent = content;
}
}
//=============================================
function TestLogin()
{
delCookie(COOKIE_NAME);
var username=getElementById('txtUserName').value;
SetCookie(COOKIE_NAME,username);
//var cookievalue=GetCookie(COOKIE_NAME);
window.location.href="9.htm?u="+username;
//alert(cookievalue);
}
</script>
</head>
<body>
<input id="txtUserName" type="text" /><input id="Button1" type="button" value="login" onclick='TestLogin();' />
</body>
</html>
9.htm
Code
<!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>
<title>JS Cookie 操作</title>
<script language="javascript" type="text/javascript">
/*
功能:取Url地址所有参数
参数:parem参数名称
*/
function GetQueryString(parem)
{
var reg = new RegExp("(^|&)"+ parem +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]);
return null;
}
var COOKIE_NAME="USERNAME"
/*
功能:保存cookies函数
参数:name,cookie名字;value,值
*/
function SetCookie(name,value){
var Days = 7; //cookie 将被保存两个月
var exp = new Date(); //获得当前时间
exp.setTime(exp.getTime() + Days*24*60*60*1000); //换成毫秒
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
/*
功能:获取cookies函数
参数:name,cookie名字
*/
function GetCookie(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null)
return unescape(arr[2]);
return null;
}
/*
功能:删除cookies函数
参数:name,cookie名字
*/
function delCookie(name){
var exp = new Date(); //当前时间
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
/*
//取文档指定Id值
*/
function isDefined(type) {
return (type != 'undefined' && type != 'unknown');
}
function childNodes(element) {
if (isDefined(typeof element.childNodes)) {
return element.childNodes;
} else if (isDefined(typeof element.children)) {
return element.children;
}
}
function getElementById(element_name) {
if (isDefined(typeof document.getElementById)) {
return document.getElementById(element_name);
} else if(typeof isDefined(document.all)) {
return document.all[element_name];
}
}
function setTextContent(elem, content) {
if (isDefined(typeof elem.innerText)) {
elem.innerText = content;
} else if (isDefined(typeof elem.textContent)) {
elem.textContent = content;
}
}
//=============================================
function testGetUrlParemValue()
{
var urlusername=GetQueryString('u');
var urltxtUser=getElementById('txtUrlUser');
urltxtUser.value=urlusername;
}
function testGetCookieValue()
{
var cookievalue=GetCookie(COOKIE_NAME);
var cookiettxtUser=getElementById('txtCookieUser');
cookiettxtUser.value=cookievalue;
}
</script>
</head>
<body>
网页传来的参数:
<input id="txtUrlUser" type="text" />
<input id="Button1" type="button" value="GetUrlValue" onclick=" testGetUrlParemValue();" /><br />
取Cookies值:
<input id="txtCookieUser" type="text" />
<input id="Button2" type="button" value="GetCookieValue" onclick="testGetCookieValue();" /><br />
</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>
<title>JS Cookie 操作</title>
<script language="javascript" type="text/javascript">
/*
功能:取Url地址所有参数
参数:parem参数名称
*/
function GetQueryString(parem)
{
var reg = new RegExp("(^|&)"+ parem +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]);
return null;
}
var COOKIE_NAME="USERNAME"
/*
功能:保存cookies函数
参数:name,cookie名字;value,值
*/
function SetCookie(name,value){
var Days = 7; //cookie 将被保存两个月
var exp = new Date(); //获得当前时间
exp.setTime(exp.getTime() + Days*24*60*60*1000); //换成毫秒
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
/*
功能:获取cookies函数
参数:name,cookie名字
*/
function GetCookie(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null)
return unescape(arr[2]);
return null;
}
/*
功能:删除cookies函数
参数:name,cookie名字
*/
function delCookie(name){
var exp = new Date(); //当前时间
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
/*
//取文档指定Id值
*/
function isDefined(type) {
return (type != 'undefined' && type != 'unknown');
}
function childNodes(element) {
if (isDefined(typeof element.childNodes)) {
return element.childNodes;
} else if (isDefined(typeof element.children)) {
return element.children;
}
}
function getElementById(element_name) {
if (isDefined(typeof document.getElementById)) {
return document.getElementById(element_name);
} else if(typeof isDefined(document.all)) {
return document.all[element_name];
}
}
function setTextContent(elem, content) {
if (isDefined(typeof elem.innerText)) {
elem.innerText = content;
} else if (isDefined(typeof elem.textContent)) {
elem.textContent = content;
}
}
//=============================================
function testGetUrlParemValue()
{
var urlusername=GetQueryString('u');
var urltxtUser=getElementById('txtUrlUser');
urltxtUser.value=urlusername;
}
function testGetCookieValue()
{
var cookievalue=GetCookie(COOKIE_NAME);
var cookiettxtUser=getElementById('txtCookieUser');
cookiettxtUser.value=cookievalue;
}
</script>
</head>
<body>
网页传来的参数:
<input id="txtUrlUser" type="text" />
<input id="Button1" type="button" value="GetUrlValue" onclick=" testGetUrlParemValue();" /><br />
取Cookies值:
<input id="txtCookieUser" type="text" />
<input id="Button2" type="button" value="GetCookieValue" onclick="testGetCookieValue();" /><br />
</body>
</html>