Script Example : Confirming a User's Choice
Phew! Congrats, you made it and nothing happened..lol, just that you witnessed the true actions of how JavaScript works and how it can open up many opportunities later in your own projects.
Below is the same information as before for you to review and try over again and again just by refreshi, and the other results. Have fun, that's what this tutorial is about and to take as much time as you need to try the scripts out.
Confirming a User's Choice
Ther are times that you want to give your user choices that they can activate and get a response or a result in figures. The links below will take you to a new page that will give you some interesting and fun results, so give them a try and after your done I will explain what took place so you don't freakout when you see all these boxes pop up. I admit it brings out a little bit of anxiety in me when ever I see a bunch of these unknown boxes pop up, so I will be there to guide you on your way:)
These scripts also introduce what we call conditionals, which simply means that the script poses a test and performs action in different states, but it will change depending on the choice. You can almost think of it as the old school "Choose Your Own Adventure" series where you would read the story and then come to an area where you would choose whether you open the door, or climb out the window, or make left turn or right turn. Either way the choice you made, changed the out come of the story. I know this is a little stretched but hopefully you'll get the point when you see the explanation.
About Conditionals
Conditionals can be broken down into three parts: if, is where we do our test; then, is where we put the script that will have the result of being true; and finally we have the optional else, is where we put the script that will have the result of being False. The section with the if part are in parentheses, and the section with the other two parts are in the braces.
1 |
if (confirm("Are you sure you want to do that?")){ |
---|---|
The confirm() method uses one parameter, and the other parameter will include the question: ("Are you sure want to do that?") and then resturns to either True or False depending on the user's choice. |
|
2 |
alert("You said Yes!"); |
If the user chose OK button, confirm() returns true, and then an alert displays, saying "You said yes". If you noticed this would be the then section of the code, even though there's no then operator in JavaScript. The braces as the delineation of the then area. |
|
3 | |
4 | |
5 | |
6 | |