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

[JavaScript] open() 함수- 새 창 열기, Date 객체

by byeolsub 2023. 4. 18.
  • open() 함수
- open 함수 : 새창을 띄워주는 함수.
  open(새창에 열린 페이지명,새창의이름,새창에적용옵션)

- nw : 새창의 window 객체임.

- nw.document : 새창의 문서객체

 

📌

<!DOCTYPE html>
<!-- src/main/webapp/20220923/javascript3.html -->
<html>
<head>
<meta charset="UTF-8">
<title>open() 함수</title>
<script type="text/javascript">
   let nw
   function open_win() {
	   //open(새창에 열린 페이지명,새창의이름,새창에적용옵션)
	   //nw : 새창의 window 객체임.
	   //nw.document : 새창의 문서객체
	   nw = window.open
	   ('','',"width=600,height=200,menubar=no,top=200,left=200")
	   nw.document.write("새로운 윈도우 입니다.<br>")
	   nw.document.write
	     ("javascript3.html 페이지의 open()로 열린창입니다..<br>")
   }
   function close_win() {
	   nw.close() //새로운 창을 닫기 
   }
   function open_win2() {
	   nw = open("javascript2.html",'',
			   "width=600,height=200,menubar=no,top=200,left=200")
	   //새창의 id값에 aaa 입력하기
	   nw.onload = function(){ //nw 윈도우의 로드가 완료되면
		   nw.document.f.id.value = 'aaa'
	   }
   }
</script>
</head>
<body>
<a href="javascript:open_win()">새창열기</a>
<a href="javascript:open_win2()">javascript2.html 페이지 새창열기</a>
<a href="javascript:close_win()">새창닫기</a>
<hr>
<form name="f">
  아이디 : <input type="text" name="id"       id="id"><br>
  비밀번호 : <input type="password" name="pw" id="pw"><br>
</form>
</body>
</html>

  • Date 객체
now.toGMTString() : 표준시. 영국시
now.toLocaleString() : 지역 일시
now.getFullYear() : 4자리 연도
now.getMonth() : 월 (0~11)
now.getDay() : 요일(0:일~6:토)

 

📌

<!DOCTYPE html>
<!--  src/main/webapp/20220923/javascript4.html -->
<html>
<head>
<meta charset="UTF-8">
<title>Date 객체</title>
</head>
<body>
<script type="text/javascript">
   now = new Date()  //현재일시
   document.write(now,"<br>")
   document.write(now.toString(),"<br>")
   document.write(now.toGMTString(),"<br>") //표준시. 영국시간
   document.write(now.toLocaleString(),"<br>") //지역일시. 
   document.write("년도:"+now.getYear(),"<br>")
   document.write("년도:"+now.getFullYear(),"<br>") //4자리 시간
   document.write("월(0~11):"+now.getMonth(),"<br>")
   document.write("일:"+now.getDate(),"<br>")
   document.write("요일(0:일~6:토):"+now.getDay(),"<br>")
   document.write("시:"+now.getHours(),"<br>")
   document.write("분:"+now.getMinutes(),"<br>")
   document.write("초:"+now.getSeconds(),"<br>")
   document.write
   ("getTime()1970년부터 현재까지 :" + now.getTime() + "msec<br><hr>")
   //now의 다음날로 설정하기
   now.setTime(now.getTime() + (1000*60*60*24))
   document.write("다음날:" + now,"<br>")
   
   //Date 객체로 일자 설정하기 : 2022년12월25일 설정하기
   date = new Date(2022,(12-1),25)
   document.write("date:"+date,"<br><hr>")
   document.write(date.toGMTString(),"<br>") //표준시. 영국시간
   
   function show_date() {
	   dateprt.innerHTML =
		  "선택된 날짜:" + document.querySelector("#date").value
   }
</script>
  <input type="date" id="date"> 
  <input type="button" value="날짜입력" onclick="show_date()">
  <div id="dateprt"></div>
</body>
</html>