본문 바로가기

프로그래밍/HTML _ CSS _ JavaScript

쿠키를 이용한 아이디 저장..


친구가 일하면서 나에게 숙제를 줘서.. 하다보니까 나중에 써먹을 일이 있을꺼 같아서..

대충 구현하고 포스팅~

------------------------------------------------------------------------------------------------
[code type=javascript]
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=euc-kr">
<title>쿠키</title>
<script language="javascript">
 //document.onreadystatechange=function(){
 // if(document.readyState == "complete") chkCookie();
 //}
 function setCookie (name, value) {
  if (confirm('ID를 저장하시겠습니까?'))
  {
   if (document.loginFrm.isCheck.checked)
   {
    var expdate = new Date();
    expdate.setTime(expdate.getTime() + 1000 * 3600 * 24 * 7);     var sTmpStr = name + "=" + escape (value) + "; expires=" + expdate;
    document.cookie = sTmpStr;
    //alert('쿠키값저장 = ' + sTmpStr);    }
  }
 }  function getCookie(Name) {
  var search = Name + "="
  if (document.cookie.length > 0) {
   offset = document.cookie.indexOf(search)
   if (offset != -1) {
    offset += search.length
    end = document.cookie.indexOf(";", offset)
    if (end == -1)
     end = document.cookie.length
    return unescape(document.cookie.substring(offset, end))
   }
  }
  return "";
 }
 function deleteCookie( cookieName ){
  document.cookie = cookieName+"=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
  location.reload();
 }  function chkCookie(){   var isCookie = document.loginFrm;
  if (getCookie('uid') != "")
  {
   document.loginFrm.isCheck.checked = true;
   document.loginFrm.uid.value=getCookie('uid');
  }
  else
  {
   document.loginFrm.uid.value='';
   document.loginFrm.isCheck.checked = false;
  }
 }
</script>
</head>
<body onload="chkCookie();"> <form name="loginFrm" action="test1.html">
 <input type="text" name="uid">
 <input type="checkbox" onclick="javascript:setCookie('uid', document.loginFrm.uid.value);" name="isCheck"><br>
 <input type="submit" value="확인">
 <input type="button" value="삭제" onclick="javascript:deleteCookie('uid');">
</form>
</body>
</html> [/code]