앞에서 지역변수 할 때
함수안에 선언하고도 다른곳에서 사용가능하게 할려면 어떻게하면 할 수 있다고 했죠?
기억하시려나?
답은 아래 예제에 있습니다. ㅎ
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko"> |
|
|
|
|
|
<head> |
|
|
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> |
|
|
|
|
|
<title> 사용자 정의 함수3. 전역변수3 </title> |
|
|
<meta name="description" content=""> |
|
|
|
|
|
<script type="text/javascript"> |
|
|
//<!CDATA[ |
|
|
|
|
|
function chk1() |
|
|
{ |
|
|
i=10; //var를 빼면 전역변수로 인식 |
|
|
alert(i); |
|
|
} |
|
|
|
|
|
function chk2() |
|
|
{ |
|
|
i=i+1; |
|
|
alert(i) |
|
|
} |
|
|
|
|
|
|
|
|
//]]]]]]> |
|
|
</script> |
|
|
|
|
|
</head> |
|
|
|
|
|
|
|
|
<body> |
|
|
|
|
|
<p><input type="button" value="버튼1" onclick="chk1()" /></p> |
|
|
<p><input type="button" value="버튼2" onclick="chk2()" /></p> |
|
|
|
|
|
</body> |
|
|
|
|
|
</html> |
