본문 바로가기
수업(국비지원)/JavaScript

[JavaScript] window 객체

by byeolsub 2023. 4. 18.
  • window 객체
- window 객체 : html 페이지의 최상단 객체. window 생략 가능
- alert(내용) : 확인 메세지. 리턴값이 없음. 
- confirm(내용) : 확인,취소 선택. 
                 확인 선택 : true
                 취소 선택 : false
- prompt(내용) : 입력란이 존재. 값을 입력받고, 입력받은 값을 리턴
- this.form : 내가 속한 form 객체를 의미                

 

📌 window 객체의 3가지 메세지 창

<!DOCTYPE html>
<!-- src/main/webapp/20220922/javascript3.html -->
<html>
<head>
<meta charset="UTF-8">
<title>window 객체의 3가지 메세지 창</title>
</head>
<body>
<form name="f">
<input type="radio" name="dbox" 
   onclick="document.f.echo.value=window.alert('alert 메세지 창 호출')"> 
   alert 메세지 창 호출<br>
<input type="radio" name="dbox" 
   onclick="this.form.echo.value=window.confirm('confirm 메세지 창 호출')"> 
   confirm 메세지 창 호출<br>
<input type="radio" name="dbox" 
   onclick="this.form.echo.value=window.prompt('prompt 메세지 창 호출')"> 
   prompt 메세지 창 호출<br>
<input type="text" name="echo">

</form>   
</body>
</html>

  • window 객체의 함수
eval 함수 : 수식 계산 결과 리턴
eval(문자열) : 문자열 수식을 계산 결과 리턴

isNaN : 숫자가 아니니?   숫자가 아닌경우 : true, 숫자인 경우 : false 
parseInt : 문자열을 정수 변환
parseInt(문자열, 진수) : 문자열을 정수 변형.

 

📌

<!DOCTYPE html>
<!-- src/main/webapp/20220922/javascript4.html -->
<html>
<head>
<meta charset="UTF-8">
<title>window 객체의 함수</title>
</head>
<body>
<script type="text/javascript">
  let i=2
  //eval 함수 : 수식 계산 함수
  document.write("eval(3+4*(5-i))="+eval(3+4*(5-i))+"<br>")
  document.write("eval(3+4*5)="+eval('3+4*5')+"<br>")
  document.write(`3+4*(5-i)=${3+4*(5-i)}<br>`) //ECMA6
  document.write("3+4*(5-i)="+`${3+4*(5-i)}` + "<br>") //ECMA6
  //isNaN : 숫자가 아니니?   숫자가 아닌경우 : true, 숫자인 경우 : false 
  document.write("isNaN('한국')="+window.isNaN('한국')+"<br>")
  document.write("isNaN('123')="+isNaN('123')+"<br>")
  //정수형 <= 문자열
  document.write("parseInt('10')="+parseInt('10')+"<br>")
  document.write("parseInt('12',8)="+parseInt('12',8)+"<br>")
  document.write("parseInt('FF',16)="+parseInt('FF',16)+"<br>")
  document.write("parseInt('FF')="+parseInt('FF')+"<br>")
</script>
</body>
</html>