2024.04.11
기본 이벤트
<script type="text/javascript">
window.onload=function(){
const alink = document.getElementById('alink');
alink.onclick=function(){
alert('이벤트 연결');
return false;
};
const myForm = document.getElementById('myForm');
myForm.onsubmit=function(){
const name = document.getElementById('name');
alert(name.value);
return false;
};
};
</script>
이벤트 전달
<script type="text/javascript">
window.onload=function(){
document.getElementById('space').onclick=function(){
alert('space');
this.style.background='pink';
};
document.getElementById('paragraph').onclick=function(event){
alert('paragraph');
this.style.background='yellow';
event.stopPropagation();
};
};
</script>
표준 이벤트 모델
<script type="text/javascript">
window.addEventListener('load',function(){
const header = document.getElementById('header');
header.addEventListener('click',function(){
alert('이벤트 연결');
},false);
},false);
</script>
예외처리
<script type="text/javascript">
document.write('1<br>');
try{
document.write('2<br>');
}catch(exception){
document.write('3<br>');
}
document.write('4<br>');
document.write('----------------<br>');
document.write('1<br>');
try{
document.write(num);
document.write('2<br>');
}catch(Exception){
document.write('3<br>');
}
document.write('4');
</script>
예외 발생 및 finally
<script type="text/javascript">
try{
let result = 10/0;
if(!isFinite(result)){
throw 'DivideByZeroException';
}
}catch(exception){
document.write(exception);
}finally{
document.write('<br>오류가 있거나 또는 없어도 반드시 수행');
}
document.write('<br>프로그램 끝!!');
</script>