WEB_TECH/javascript

pop up boxes

지식사랑 2012. 8. 17. 13:02

1. Alert Box

경고창입니다. 

alert("sometext");


예제

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript">

function myFunction()

{

alert("Hello! I am an alert box!");

}

</script>

</head>

<body>


<input type="button" onclick="myFunction()" value="Show alert box" />


</body>

</html>


줄바꿈을 넣어보겠습니다.

alert("Hello\nHow are you?");

\n 을 넣으면 줄바꿈이 일어납니다.



2. Confirm Box

ok, cancel 버튼을 클릭하면 true, flase값 반환

confirm("sometext");


예제

<!DOCTYPE html>

<html>

<body>


<p>Click the button to display a confirm box.</p>


<button onclick="myFunction()">Try it</button>


<p id="demo"></p>


<script type="text/javascript">

function myFunction()

{

var x;

var r = confirm("Press a button!");

if (r == true)

{

  x="You pressed OK!";

}

else

{

  x="You pressed Cancel!";

}

document.getElementById("demo").innerHTML=x;

}

</script>


</body>

</html>



3. Prompt Box

ok버튼 클릭하면 인풋값이 들어간 후에 진행되고, 

cancel 버튼 글릭하면 null값을 리턴합니다.

prompt("sometext","defaultvalue");


예제

<!DOCTYPE html>

<html>

<body>


<p>Click the button to demonstrate the prompt box.</p>


<button onclick="myFunction()">Try it</button>


<p id="demo"></p>


<script type="text/javascript">

function myFunction()

{

var x;


var name=prompt("Please enter your name","Harry Potter");


if (name!=null)

{

x="Hello " + name + "! How are you today?";

document.getElementById("demo").innerHTML=x;

}

}

</script>


</body>

</html>