When browsing a webpage, some information is often displayed. Some of this information is directly displayed on the webpage, and some are displayed in the form of a dialog box. How can this information be achieved?
The three pop-up dialog boxes in JavaScript use the alert (), confirm (), and prompt () methods. if you do not want to use the pop-up dialog box, you can also use document. write () directly displays the information on the page.
First look at the three pop-up boxes and write display effect.
Dialog Box: Firefox 21.0
IE8.0
|
|
Confirm () |
|
|
Prompt () |
|
|
Document. write () |
|
|
Alert ()
Confirm ()
Prompt ()
Document. write ()
I have read the above table, but it is still confusing. The differences between the display information are as follows.
First, the alert (), confirm (), and prompt () dialog boxes are displayed. among them, alert () is the simplest, and the effect of write () is similar to that displayed on the page. There is only one OK button. the difference between it and write () is that it is interactive, and the program will not continue without the user clicking OK, but after the write () shows the information, the subsequent operations will continue.
Alert is often used when writing code. It is mainly used to display certain results to see if it is as expected. or write a function to see if it will be executed as expected. the syntax is alert (str), and str is the string or variable used for display.
Confirm (str), confirmation dialog box. it is different from alert in that it only gives the user a chance to choose and can be confirmed or canceled. alert is a reminder and a warning, while confirm is a choice. Therefore, you can perform different operations based on your selection.
Prompt (text, defaultText), text is the plain text displayed in the dialog box, that is, "I am the prompt of prompt ()"; defaultText, optional, enter... ", is the default input text; if no default input text is available, it is written as" "; otherwise, the text box in IE will display undefined. example
[Javascript]
Prompt ("12 ","");
Prompt ("12"); // IE displays undefined
Prompt ("12 ","");
Prompt ("12"); // IE displays undefined
Write (str), str is also used to display strings, but because it is displayed on the page, you can add some html code to it with the format.
[Javascript]
<Script language = "javascript" type = "text/javascript">
Var a1 = 1;
Document. write (a1 );
Document. write ("<table width = '000000' border = '1'> <tr> <td> 2 </td> <td> 3 </td> </tr> </table> ");
Document. write ("hello, this is the display effect of document. write ");
Document. write ("</Script>
<Script language = "javascript" type = "text/javascript">
Var a1 = 1;
Document. write (a1 );
Document. write ("<table width = '000000' border = '1'> <tr> <td> 2 </td> <td> 3 </td> </tr> </table> ");
Document. write ("hello, this is the display effect of document. write ");
Document. write ("</Script>
The preceding four types of alert, confirm, and write parameters are both strings and variables. However, prompt has two parameters, even if one is a null string, the two parameters cannot be variables.