The Javascript alert, confirm, and prompt boxes

The alert, confirm, and prompt boxes

The three "commands" involved in creating alert, confirm, and prompt boxes are:

  • window.alert()
  • window.confirm()
  • window.prompt()

Lets look at them in detail. The first one is:

window.alert()

This command pops up a message box displaying whatever you put in it. For example:

<body>
<script type="text/javascript">
window.alert("My name is George. Welcome!")
</script>
</body>

As you can see, whatever you put inside the quotation marks, it will display it.

The second one is:

window.confirm()

Confirm is used to confirm a user about certain action, and decide between two choices depending on what the user chooses.

Click here for output: 

<script type="text/javascript">
var x=window.confirm("Are you sure you are ok?")
if (x)
window.alert("Good!")
else
window.alert("Too bad")
</script>

There are several concepts that are new here, and I'll go over them. First of all, "var x=" is a variable declaration; it declares a variable ("x" in this case) that will store the result of the confirm box. All variables are created this way. x will get the result, namely, "true" or "false". Then we use a "if else" statement to give the script the ability to choose between two paths, depending on this result. If the result is true (the user clicked "ok"), "good" is alerted. If the result is false (the user clicked "cancel"), "Too bad" is alerted instead. (For all those interested, variable x is called a Boolean variable, since it can only contain either a value of "true" or "false").

The third one is:

window.prompt()

Prompt is used to allow a user to enter something, and do something with that info:

Click here for output: 

<script type="text/javascript">
var y=window.prompt("please enter your name")
window.alert(y)
</script>
 
REF:

http://www.javascriptkit.com/javatutors/alert1.shtml

http://www.javascriptkit.com/javatutors/alert2.shtml

http://www.javascriptkit.com/javatutors/alert3.shtml

http://javascript.about.com/library/bldialog.htm

http://ncthakur.itgo.com/js02.htm

http://www.aspnetcenter.com/cliktoprogram/javascript/alert.asp

posted @   emanlee  阅读(486)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示