JavaScript popup

You can create three kinds of message boxes in JavaScript: warning box, confirmation box, and prompt box.

JavaScript alert()

Syntax: alert("sometext");

at Use the alert command in JavaScript to create a message warning box:

<script type="text/javascript">
alert("I am the prompt text!");
< ;/script>

The message warning box is in dialog mode. When the warning box appears, the user needs to click the OK button to close the warning box before continuing the operation. The

alert command has one parameter, which is the text string you want to display to the user, which is not in HTML format.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function myFunction(){
   alert("我是一个警告框!");
}
</script>
</head>
<body>
   <input type="button" onclick="myFunction()" value="警告框" />
</body>
</html>

JavaScript confirm()

Use the confirm command in JavaScript to create a message confirmation box:

Syntax:confirm("sometext");

The confirmation box is usually used to verify whether the user operation is accepted.

When the confirmation card pops up, the user can click "Confirm" or "Cancel" to confirm the user operation.

When you click "Confirm", the confirmation box returns true. If you click "Cancel", the confirmation box returns false.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
<script>
    if (confirm("您确认要删除该条信息吗?")){
    alert("您选择了删除!");
    } else {
    alert("您选择了不删除");
    }
</script>
</head>
<body>
</body>
</html>

JavaScript prompt()

Syntax: prompt("sometext","defaultvalue");


Syntax description

prompt() allows two parameters: the first parameter is the prompt text to be displayed, and the second parameter is the default input character. If the input box is not empty, prompt() returns the value of the input box (HTML format is supported), otherwise it returns null.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
<script>
    var name = prompt("请输入您的名字:","默认字符")
    if (name!=null && name!=""){
    document.write("你好," + name + "!")
    }
</script>
</head>
<body>
</body>
</html>

Line break

The pop-up window uses backslash + "n"(\n) to set line break.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
    <button onclick="myFunction()">点击显示</button>
    <p id="demo"></p>
<script>
    function myFunction(){
    alert("Hello\nHow are you?");
    }
</script>
</body>
</html>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <button onclick="myFunction()">点击显示</button> <p id="demo"></p> <script> function myFunction(){ alert("我是当前显示文字\n我是换行文字"); } </script> </body> </html>
submitReset Code