관리 메뉴

nkdk의 세상

JS :: 자동완성기능 본문

My Programing/HTML and JavaScript

JS :: 자동완성기능

nkdk 2008. 6. 16. 10:32

이메일주소와 같은 보안을 요구하지 않는 내용을 한번 입력하면 그 다음부터는 자동으로 입력되도록 해주는 방법입니다.

<html>
<script language="JavaScript">
<!--

function getcookieval(offset) {  // 밸류값출력
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1)
  endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function getcookie(name) {  // 해당네임의 밸류값검색
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;

while (i < clen) {
  var j = i + alen;

  if (document.cookie.substring(i, j) == arg)
   return getcookieval(j);

  i = document.cookie.indexOf(" ", i) +1;
  if (i == 0)
   break;
}

return null;
}

function setcookie(name, value) {  // 입력값을 쿠키로 저장
var argv = setcookie.arguments;
var argc = setcookie.arguments.length;

var expires = (2<argc) ? argv[2] : null;
var path = (3<argc) ? argv[3] : null;
var domain = (4<argc) ? argv[4] : null;
var secure = (5<argc) ? argv[5] : false;

info_cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");

document.cookie = info_cookie;
return info_cookie;
}

function read_save (frms, info_sel) {  // 쿠키저장, 읽기
if (info_sel == "read") {
  if (getcookie(frms.name) != null)
   frms.value = getcookie(frms.name);
} else {
  var myday = new Date();

  myday.setTime(myday.getTime() + (30*24*60*60*1000));
  setcookie(frms.name, frms.value, myday, "/", null, false);
}
}

// -->
</script>
<body>
<div align="center">

<table border=1 cellpadding=5 cellspacing=1 bordercolor="#7ab4fc" style="font-size:9pt">
<form name="frm1" method="get">
<tr>
<td bgcolor="#ace4fc">이메일</td>
<td>
  <input type="text" name="myemail" maxlength=50 size=50
   onblur="read_save(frm1.myemail, 'save')"
   onfocus="read_save(frm1.myemail, 'read')">
</td>
</tr>
<tr>
<td bgcolor="#ace4fc">연락처</td>
<td>
  <input type="text" name="mytel" maxlength=15 size=15
   onblur="read_save(frm1.mytel, 'save')"
   onfocus="read_save(frm1.mytel, 'read')">
</td>
</tr>
</form>
</table>

</div>
</body>
</html>