2007/11/15

홈페이지 로딩시 textArea에 focus

단순히 아래와 같이 사용하면됨.




<form ... name="myform">
<input name="myfield" ...>
...
</form>
<script type="text/javascript">
document.myform.myfield.focus();
</script>


하지만 로딩중 해당 text영역을 클릭하면 로딩이 완료된후 커서가 처음으로 돌아가므로 아래와 같이 변경하면 더 좋음




<html>
<head>
<script type="text/javascript">

var formInUse = false;

function setFocus()
{
if(!formInUse) {
document.theForm.text1.focus();
}
}

</script>
</head>

<body onload="setFocus()">
<form name="theForm">
<input type="text" name="text1" onfocus="formInUse = true;">
<input type="text" name="text2" onfocus="formInUse = true;">
<input type="text" name="text3" onfocus="formInUse = true;">
</form>
<img src="images/hugePicture.jpg" alt="huge image to slow down
the loading process!">
</body>
</html>

0 comments:

댓글 쓰기