confirm method
Let's learn about the confirm method.
This method is what we use when making choices. For example, do you really want to delete some posts? You may have seen the phrase. That is the confirm method.
Have you seen a lot?
It's very simple to float this.
JavaScript
confirm("phrase to display");
Let's actually apply it
Source
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JavaScript</title> <script> confirm("Hello"); </script> </head> <body> </body> </html>
What value does the confirm() function return?
The confirm() function returns a Boolean value.
Boolean values are true and false. ^^
Let's experiment by declaring a variable and adding a confirm function to the value of the variable.
About what value to print when you press Cancel and OK.
JavaScript
var con_test = confirm("What's the value? Press OK."); document.write(con_test);
Source
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JavaScript</title> <script> var con_test = confirm("What's the value? Press OK."); document.write(con_test); </script> </head> <body> </body> </html>
If you click the button for the confirm() function, if you click the OK button on the screen, true is displayed, and if you click Cancel, false is output.
Use if statements to process statements on results
In the above source, after declaring the variable con_test and inserting the confirm() function as the value, the result is assigned to con_test. And using document.write to print out the value.
You can use an if statement to give a conditional statement: con_test == true or con_test == false. Let's do it.
JavaScript
var con_test = confirm("What's the value? Press OK."); if(con_test == true){ document.write("You pressed OK"); } else if(con_test == false){ document.write("You've pressed cancel."); }
Then let's check it through an example.
Source
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JavaScript</title> <script> var con_test = confirm("What's the value? Press OK."); if(con_test == true){ document.write("You pressed OK""); } else if(con_test == false){ document.write("You've pressed cancel."); } </script> </head> <body> </body> </html>
In the next lesson, we will learn how to create our own functions. ^^