六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 31|回复: 0

javascript 操作cookie实例

[复制链接]

升级  40%

4

主题

4

主题

4

主题

童生

Rank: 1

积分
20
 楼主| 发表于 2013-1-27 04:46:52 | 显示全部楼层 |阅读模式
 

<html><head><script type="text/javascript">function getCookie(c_name){if (document.cookie.length>0){c_start=document.cookie.indexOf(c_name + "=")if (c_start!=-1){c_start=c_start + c_name.length+1c_end=document.cookie.indexOf(";",c_start)if (c_end==-1) c_end=document.cookie.lengthreturn unescape(document.cookie.substring(c_start,c_end))}}return ""}function setSubmit(){    var userName = document.getElementById("userName").value;    setCookie('username',userName,365) document.getElementById("info").innerText="成功设置了cookie,请刷新或重启浏览器";}function setCookie(c_name,value,expiredays){var exdate=new Date()exdate.setDate(exdate.getDate()+expiredays)document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())}function checkCookie(){username=getCookie('username')if (username!=null && username!="")  { document.getElementById("userName").value=username;document.getElementById("password").focus();document.getElementById("info").innerText="发现cookie,你可以修改和删除cookie了";  }else  {  document.getElementById("info").innerText="没有发现cookie,请输入用户名点提交";  document.getElementById("userName").focus();  }}function deleteCookie(){             document.getElementById("info").innerText="cookie被删除了";     var name="username";var date=new Date(); date.setTime(date.getTime()-10000); document.cookie=name+"=v; expire="+date.toGMTString(); }</script></head><body ><form name="myform" action="hml.jsp" method="get"  >用户名:<input type="text" id="userName"><br></br>        密码:<input type="password" id="password"><br></br><input type="button" id="button" value="提交" /><input type="button" id="button2"  value="删除" /><div id="info"></div></form></body></html> 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表