2024.04.08
instanceof
<script>
function Student(name){
this.name = name;
}
let student = new Student('홍길동');
document.write(student instanceof Student);
document.write('<br>');
document.write(student instanceof Object);
document.write('<br>');
document.write(student instanceof Number);
document.write('<br>');
</script>
Client Object
<script>
window.alert('경고창');
let season = window.prompt('좋아하는 계절은?','');
document.write('좋아하는 계절은 '+season+'이다<br>');
let choice = window.confirm('야근을 하겠습니까?');
if(choice) {
document.write('야근 확정!');
} else {
document.write('야근 취소!');
}
</script>
window 새 창 열고 닫기
let win;
function winOpen(){
win = window.open('<<a href=https://www.naver.com>https://www.naver.com</a>>','child','toolbar=no,location=no',
'status=no,menubar=no,resizable=no,scrollbars=no,width=400,height=300');
}
function winClose(){
win.close();
}
일정 시간 후 한번 함수 실행 or 마다 반복 함수 실행
<script>
window.onload = function(){
alert('경고창을 닫고 3초 후 이 페이지는 종료됩니다.');
window.setTimeout(function(){
window.close();
},3000);
};
</script>
location
<script>
document.write('location,href : ' + location.href + '<br>');
document.write('location.protocol : ' + location.protocol + '<br>');
document.write('location.host : ' + location.host + '<br>');
document.write('location.hostname : ' + location.hostname + '<br>');
document.write('location.port : ' + location.port + '<br>');
document.write('location.pathname : ' + location.pathname);
<input type="button" value="이동(href)" onclick="location.href='s01_window.html'">
<input type="button" value="이동(assign)" onclick="location.assign('s02_window.html')">
<input type="button" value="이동(replace)" onclick="location.replace('s02_window.html')">
<input type="button" value="새로고침" onclick="location.reload()">
</script>
history
<body>
<input type="button" value="이전" onclick='history.back()'>
<input type="button" value="이전(go(-1))" onclick='history.go(-1)'>
<input type="button" value="다음(forward)" onclick='history.forward()'>
<input type="button" value="다음(go(1))" onclick='history.go(1)'>
</body>
Array
<script>
const array1 = new Array();
document.write(array1 + '<br>');
document.write(array1.length + '<br>');
const array2 = new Array(10);
document.write(array2 + '<br>');
document.write(array2.length + '<br>');
const array3 = new Array(52,273,103,57,32);
document.write(array3 + '<br>');
document.write(array3.length);
</script>
String
<script>
let msg = '세상의 끝에서 세상을 노래하다';
document.write(msg.length + '<br>');
document.write(msg.charAt(8) + '<br>');
document.write(msg.indexOf('상') + '<br>');
document.write(msg.indexOf('노래' + '<br>'));
document.write(msg.lastIndexOf('상'));
document.write('------------------------<br>');
document.write(msg.substring(5) + '<br>');
document.write(msg.substr(5) + '<br>');
document.write(msg.slice(5));
document.write('------------------------<br>');
document.write(msg.substring(1,5) + '<br>');
document.write(msg.substr(5,2) + '<br>');
document.write(msg.slice(1,5) + '<br>');
let msg2 = 'Hello WORLD';
document.write(msg.toUpperCase() + '<br>');
document.write(msg.toLowerCase() + '<br>');
let msg3 = '하하';
document.write(msg.concat(msg3));
document.write('------------------------<br>');
let msg4 = '2024-04-08';
let array = msg4.split('-');
document.write(array);
</script>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function check(){
let email = document.form1.email.value;
let n = email.indexOf('@');
alert(n);
if(n>0){
document.form1.id.value = email.substr(0,n);
} else {
document.form1.email.value = '';
alert('이메일 형식에 맞게 입력하세요.');
}
}
</script>
</head>
<body>
<form name="form1">
id : <input type="text" name="id" placeholder="아이디는 이메일에서 추출" readonly="readonly">
Email : <input type="text" name="email"><br>
<input type="button" value="입력" onclick="check()">
</form>
</body>
Date
<script>
const now = new Date();
document.write('now의 값 : ' + now + "<br>");
document.write("now.toString() : " + now.toString());
document.write('오늘 : ' + now.toLocaleString());
document.write('오늘의 날짜 : ' + now.toLocalDateString +'<br>');
document.write('오늘의 시간 : ' + now.toLocalTimeString +'<br>');
document.write('연도 : ' + now.getFullYear()+'<br>');
document.write('월 : ' + (now.getMonth+1)+'<br>');
document.write('일 : ' + now.getDate('<br'));
document.write('요일 : ' + now.getDay()+'<br>');
document.write("시 : " + now.getHours()+'<br>');
document.write("분 : " + now.getMinutes()+'<br>');
document.write("초 : " + now.getseconds()+'<br>');
document.write("밀리초 : " + now.getMilliseconds()+'<br>');
</script>
tagName
<script>
window.onload=function(){
const header1 = document.getElementsByTagName('태그명');
header1[0].innerHTML = '달빛이 찬란한 호수';
header1[1].innerHTML = '흰 눈이 눈 부신 들판';
const header3 = document.getElementsByTagName('태그명');
for(let i=0; i<header3.length; i++){
if(i%2==1){
header3[i].innerHTML = '우주';
} else {
header3[i].innerHTML = '지구';
}
}
}
</script>
id를 이용한 문서 객체 탐색
<script>
window.onload=function(){
const header1 = document.getElementById('아이디명');
const header2 = document.getElementById('아이디명');
header1.innerHTML = '하하';
header2.innerHTML = '호호';
}
</script>
태그명과 id를 사용해서 문서 객체 탐색
<script>
window.onload = function(){
const allSpans = document.getElementsByTagName('span');
let output = '[모든 span 태그의 text 출력]\\n';
for(let i=0; i<allSpans.length; i++){
output += allSpans[i].innerHTML + '\\n';
}
alert(output);
const foo = document.getElementById('foo');
const fooSpans = foo.getElementsByTagName('span');
let output2 = '[id가 foo인 p태그 하위 태그 중 모든 span 태그의 text 출력]\\n';
for(let i=0;i<fooSpans.length;i++){
output2 += fooSpans[i].innerHTML + '\\n';
}
alert(output2);
};
</script>
태그명을 통해 태그 객체들의 정보 처리
<script>
window.onload=function(){
const prev = document.getElementsByName('prev');
prev[0].innerHTML = '이전';
const next = document.getElementsByName('next');
next[0].innerHTML = '다음';
const country = document.getElementsByName('country');
country[0].checked=true;
const comment = document.getElementsByName('comment');
comment[0].value = '간단한 설명 입력';
}
</script>