Home Web Front-end JS Tutorial What are the JavaScript pop-up boxes?

What are the JavaScript pop-up boxes?

Apr 07, 2021 pm 06:09 PM
javascript Pop-up box

JavaScript pop-up boxes include: 1. Alert box, which has only one button "OK" and no return value. It is often used to ensure that users can get certain information; 2. Confirmation box, which has two buttons: "OK" and "Cancel" A button, which returns a true or false value, is often used to allow users to verify or accept certain information; 3. Prompt box, which returns the entered message.

What are the JavaScript pop-up boxes?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The three dialog boxes of JavaScript are obtained by calling the three methods alert(), confirm() and prompt() of the window object. You can use these dialog boxes to complete js Input and output, implement js code that can interact with users.

Today the editor will briefly introduce the three pop-up dialog boxes in js. The editor will first explain these methods in detail separately, and then compare these methods. Okay, let’s start. Our js journey `(*∩_∩*)′...

First: alert() method

The alert() method is the easiest to use among these three dialog boxes. It can be used to simply and clearly display the text information in the alert() brackets in the dialog box. We call it an alert dialog box. The information to be displayed is placed in brackets, and the dialog box contains a "Confirm" button that the user can simply click to close the dialog box after reading the displayed information. Let's look at an example of using the alert() method. The code is as follows:

<html>
<head>
<title>编写html页面</title>
<script language="javascript"> //JavaScript脚本标注
alert("上联:山石岩下古木枯");//在页面上弹出上联
alert("下联:白水泉边少女妙");//在页面上弹出下联
</script>
</head>
</html>
Copy after login

Execute the above small example, a dialog box will pop up on the page and the sentence "Shanglian: Ancient trees are dead under the rocks", As shown below:

What are the JavaScript pop-up boxes?

Then, click the "Confirm" button and then the second dialog box will be displayed and "The girl beside the white water spring is wonderful!", the effect is as follows;

What are the JavaScript pop-up boxes?

A dialog box pops up on the page and displays the sentence "Shanghai Lien: The ancient trees are dead under the rocks." After clicking the "Confirm" button, the second dialog box is displayed and displayed. "The girl beside the white water spring is wonderful!" Let's analyze this small example:

a. Call the alert() method twice in the <script> script block; </p><p>b. In each A piece of text information is added to each alert() bracket. When running, the page shown below will appear. When you click the "OK" button on the page with the mouse, the second page will appear. Click the "OK" button again. Close the dialog box on the page. Note: The two dialog boxes are displayed separately, rather than one covering the other. This is because js actually executes the first alert() and waits until the user clicks the "Confirm" button before executing the second alert(). . </p><p>alert() is a method of the js window object. When called, it can be written as window.alert() or alert(). Its function is to generate a dialog box with a confirmation button and brackets displayed above. Information within, </p><p>[Recommended learning: <a href="https://www.php.cn/course/list/2.html" target="_blank">javascript advanced tutorial</a>]</p><p><strong>Second: confirm() method</strong></p><p> The confirm() method is very similar to the alert() method. The difference is that in addition to a "confirm" button, this type of dialog box also has a "cancel" button. This type of dialog box is called a confirmation dialog box. You do not need to write window when calling the confirm() method of the window object and the prompt() method introduced later. Let's look at a small example about confirm(). The code is as follows: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;html&gt; &lt;head&gt; &lt;title&gt;编写html页面&lt;/title&gt; &lt;script language=&quot;javascript&quot;&gt; //js脚本标注 confirm(&quot;上联:一但重泥拦子路;下联:两岸夫子笑颜回&quot;); //在页面上弹出确认对话框 &lt;/script&gt; &lt;/head&gt; &lt;/html&gt;</pre><div class="contentsignin">Copy after login</div></div><p>The display effect is as follows: </p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/889/299/621/1617957644198830.png" class="lazy" title="161795763180077What are the JavaScript pop-up boxes?" alt="What are the JavaScript pop-up boxes?"/></p><p>Analyze this small example:</p><p>a. Add the confirm() method in the <script> script block. </p><p>b. Add a piece of text information in the confirm() brackets. The running effect is as shown in the figure above. If the user When the "Confirm" button is clicked, the confirm() method will return true. If the user clicks the "Cancel" button, the confirm() method will return false. No matter which button the user selects, the dialog box will be closed and the JavaScript code will continue to be executed. . Clicking the "Confirm" or "Cancel" button both closes the dialog box. There seems to be no difference. In fact, whether clicking the "Confirm" or "Cancel" button will return a boolean value, so that there can be some js behind the scenes. Code to play the role of the button. Please look at the example below to experience the beauty of using confirm() to return a Boolean value. The code is as follows: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;html&gt; &lt;head&gt; &lt;title&gt;编写html页面&lt;/title&gt; &lt;script language=&quot;javascript&quot;&gt; //js脚本标注 var con; con=confirm(&quot;你喜欢玫瑰花么?&quot;); //在页面上弹出对话框 if(con==true)alert(&quot;非常喜欢!&quot;); else alert(&quot;不喜欢!&quot;); &lt;/script&gt; &lt;/head&gt; &lt;/html&gt;</pre><div class="contentsignin">Copy after login</div></div><p> Let’s analyze this small example: </p><p>a. A variable con is declared in the <script> script block. </p><p>b. The sentence con=confirm() assigns the Boolean value returned by the confirm() method to con. </p><p>c. Use the value of con through the if statement to execute different statements respectively; the execution effect is as follows: </p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/889/299/621/1617957644198830.png" class="lazy" title="161795764032679What are the JavaScript pop-up boxes?" alt="What are the JavaScript pop-up boxes?"/></p><p>If you click the confirmation box on the page After clicking the "OK" button on the screen, a page as shown below appears: </p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/889/299/621/1617957644198830.png" class="lazy" title="1617957644198830.png" alt="What are the JavaScript pop-up boxes?"/></p><p>如果单击“取消”按钮,则出现如下图所示的页面:</p><p><img src="https://img.php.cn/upload/image/930/744/839/161795764822891What are the JavaScript pop-up boxes?" title="161795764822891What are the JavaScript pop-up boxes?" alt="What are the JavaScript pop-up boxes?"/></p><p><strong>第三种: prompt()方法</strong></p><p>alert()方法和confirm()方法的使用十分类似,都是仅仅显示已有的信息,但用户不能输入自己的信息,但是prompt()可以做到这点,她不但可以显示信息,而且还提供了一个文本框要求用户使用键盘输入自己的信息,同时她还包含“确认”或“取消”两个按钮,如果用户“确认”按钮,则prompt()方法返回用户在文本框中输入的内容(是字符串类型)或者初始值(如果用户没有输入信息);如果用户单击“取消”按钮,则prompt()方法返回null,我们称这种对话框为提示框,在这三种对话框中,她的交互性最好。</p><p>看下面一个小例子:在页面上两次弹出提示对话框,使用户能输入有关信息,代码如下:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;html&gt; &lt;head&gt; &lt;title&gt;编写html页面&lt;/title&gt; &lt;script language=&quot;javascript&quot;&gt; //js脚本标注 var name,age; name=prompt(&quot;请问你叫什么名字?&quot;); /*在页面上弹出提示对话框, 将用户输入的结果赋给变量name*/ alert(name); //输出用户输入的信息 age=prompt(&quot;你今年多大了?&quot;,&quot;请在这里输入年龄&quot;); /*在页面上再一次弹出提示对话框, 讲用户输入的信息赋给变量age*/ alert(age)//输出用户输入的信息 &lt;/script&gt; &lt;/head&gt; &lt;/html&gt;</pre><div class="contentsignin">Copy after login</div></div><p>运行上面的程序,效果如下所示:</p> <p><img src="https://img.php.cn/upload/image/251/610/632/161795765680633What%20are%20the%20JavaScript%20pop-up%20boxes?" title="161795765680633What are the JavaScript pop-up boxes?" alt="What are the JavaScript pop-up boxes?"></p> <p>点击确定,会有这么惊喜nie:</p> <p><img src="https://img.php.cn/upload/image/610/774/956/161795766013843What%20are%20the%20JavaScript%20pop-up%20boxes?" title="161795766013843What are the JavaScript pop-up boxes?" alt="What are the JavaScript pop-up boxes?"></p> <p>我们再点击确定按钮:</p> <p><img src="https://img.php.cn/upload/image/536/273/530/161795766318481What%20are%20the%20JavaScript%20pop-up%20boxes?" title="161795766318481What are the JavaScript pop-up boxes?" alt="What are the JavaScript pop-up boxes?"></p> <p>再点击确定按钮:</p> <p><img src="https://img.php.cn/upload/image/489/290/460/161795767331543What%20are%20the%20JavaScript%20pop-up%20boxes?" title="161795767331543What are the JavaScript pop-up boxes?" alt="What are the JavaScript pop-up boxes?"></p> <p>分析一下这个小例子</p> <p>a、在<script>脚本块中添加了两个prompt()方法。</script>

b、在第一个prompt()括号内添加了一段文本信息。

c、name=prompt()一句是将用户在文本框中输入的信息赋给变量name。

alert()、confirm()、prompt()的区别和联系:

警告框alert()

alert是警告框,只有一个按钮“确定”无返回值,警告框经常用于确保用户可以得到某些信息。当警告框出现后,用户需要点击确定按钮才能继续进行操作。语法:alert("文本")。

确认框confirm()

confirm是确认框,两个按钮,确定或者取消,返回true或false。确认框用于使用户可以验证或者接受某些信息。当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。语法:confirm("文本")

提示框prompt()

prompt是提示框,返回输入的消息,或者其默认值提示框经常用于提示用户在进入页面前输入某个值。当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。语法:prompt("文本","默认值")

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of What are the JavaScript pop-up boxes?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles