Home >Web Front-end >JS Tutorial >Knowledge about JavaScript pop-up events
JavaScript pop-up events play an important role in warnings, verifications, etc. This article will give you some understanding of them.
JavaScript pop-up window
You can create three kinds of message boxes in JavaScript: warning box, confirmation box, and prompt box.
Alert box
Alert boxes are often used to ensure that users can get certain information.
When the warning box appears, the user needs to click the OK button to continue the operation.
Syntax
window.alert("sometext");
The window.alert() method can be used directly without the window object. The alert() method can be used directly.
Example
<!DOCTYPE html> <html> <head> <script> function myFunction() { alert("你好,我是一个警告框!"); } </script> </head> <body> <input type="button" onclick="myFunction()" value="显示警告框"> </body> </html>
Confirmation box
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.
Syntax
window.confirm("sometext");
The window.confirm() method can be used directly without the window object, using the confirm() method.
Example
var r=confirm("按下按钮"); if (r==true) { x="你按下了\"确定\"按钮!"; } else { x="你按下了\"取消\"按钮!"; }
Prompt box
Prompt box is often used to prompt the user to enter a certain value before entering the page.
When the prompt box appears, the user needs to enter a certain value and then click the confirm or cancel button to continue the operation.
If the user clicks to confirm, the return value is the entered value. If the user clicks Cancel, the return value is null.
Syntax
window.prompt("sometext","defaultvalue");
The window.prompt() method can be used directly without the window object. The prompt() method can be used directly.
Example
var person=prompt("请输入你的名字","Harry Potter"); if (person!=null && person!="") { x="你好 " + person + "! 今天感觉如何?"; document.getElementById("demo").innerHTML=x; }
Line break
The pop-up window uses backslash "n"(\n) to set line break.
Example
alert("Hello\nHow are you?");
js pop-up event The above has made some simple understandings. For more learning materials, please pay attention to the php Chinese website to view.
Related recommendations:
About the use of Node.js time loop
About the actual application method of jQuery Callback function
About how to use jQuery stop()
The above is the detailed content of Knowledge about JavaScript pop-up events. For more information, please follow other related articles on the PHP Chinese website!