알쓸전컴(알아두면 쓸모있는 전자 컴퓨터)

[javascript] alert,confirm,prompt 사용하기 본문

Web /웹브라우저 객체 API

[javascript] alert,confirm,prompt 사용하기

백곳 2017. 8. 19. 09:59

alert

경고창이라고 부른다. 사용자에게 정보를 제공하거나 디버깅등의 용도로 많이 사용한다.

<!DOCTYPE html>
<html>
    <body>
        <input type="button" value="alert" onclick="alert('hello world');" />
    </body>
</html>


confirm

<!DOCTYPE html>
<html>
    <body>
        <input type="button" value="alert" onclick="func_confirm()" />
        <script type="text/javascript">
        	function func_confirm () {
        		if(confirm('ok?')){
        			alert("ok");
        		} else {
        			alert("cancle");
        		}
        	}
        </script>
    </body>
</html>

confirm('ok?') 은 yes을 누를 경우 리턴으로 true를 하고 cancle 을 누를 경우에 false 를 리턴 합니다. 


prompt

<!DOCTYPE html>
<html>
    <body>
        <input type="button" value="alert" onclick="func_prompt()" />
        <script type="text/javascript">
        	function func_prompt () {
        		if(prompt('id')==='12345'){
        			alert("12345");
        		} else {
        			alert("false");
        		}
        	}
        </script>
    </body>
</html>

리턴 값으로는 입력하고 확인 버튼을 누른 값으로 리턴합니다.



Comments