[javascript-tip][ie7,ff3]javascript를 이용한 글자 수 알아내기(byte)-입력시 바로 알아내기(timer 이용)
<script language="javascript">
<!--
var timerBytes;
/*----------------------------------------------------------------------------------------
// 타이머 없애기
----------------------------------------------------------------------------------------*/
function clearTimerStrBytes()
{
clearTimeout(timerBytes);
}
/*----------------------------------------------------------------------------------------
// 계산 : 글자 수(타이머를 이용한 byte 방식)
----------------------------------------------------------------------------------------*/
function setTimerStrBytes(objThis,eid)
{
if(document.getElementById(objThis.id) == null)
{
return;
}
var str = document.getElementById(objThis.id).value;
var l = 0;
for(var i=0, c; i<str.length; i++)
{
c=str.charCodeAt(i);
if(c > 128 )
{
l += 2;
}
else
{
l += 1;
}
}
document.getElementById(eid.id).innerHTML = l;
var fn = function()
{
setTimerStrBytes(objThis,eid);
}
timerBytes = setTimeout(fn,100);
}
//-->
</script>
<table width="300" height="40" border="0" cellpadding="0" cellspacing="0">
<tr height="10">
<td></td>
</tr>
<tr>
<td><font color="#6e75c1">입력 글자 수 : <font style="font-size:11px">(<span id="divStrLength" name="divStrLength">0</span>/255)</font></font></td>
</tr>
<tr>
<td><input type="text" id="tareaMemo" name="tareaMemo" onfocus="setTimerStrBytes(document.getElementById('tareaMemo'),document.getElementById('divStrLength'));" onblur="clearTimerStrBytes();" onkeypress="if(event.keyCode==13){clearTimerStrBytes();}" style="width:250px; height:20px; border:1px solid #a5aded"></td>
</tr>
</table>