The Browser Object Model (BOM) continuation.. 30daysofJavaScript ==> Day 21
In the last episode we talked about objects, variables and methods of the browser object model, we would conclude other examples of this BOM in the article.
JavaScript Popup Boxes
Whether you are playing an online game, shopping or just searching for some sort of information, you have experienced these pop-up boxes. JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
- Alert Box
An alert box is often used if you want to make sure information comes through to the user. It is like double-checking to be sure if you locked the front door of your home. When an alert box pops up, the user will have to click “OK” to proceed. The window.alert()
method can be written without the window prefix.
- Confirm Box
A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either “OK” to proceed or “Cancel” not to. If “OK” is, the box returns true otherwise the box returns false. The window.confirm()
method can be written without the window prefix.
- Prompt Box
Often times the prompt box is used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value. If the user clicks “OK” the box returns the input value. If the user clicks “Cancel” the box returns null.
Timing Events
You can determine when a code is executed, how long the code is executed for and how many times it can be executed. This activity is known as timing events. There are two methods of this known as :
setTimeout(function, milliseconds
)
Executes a function, after waiting a specified number of milliseconds.
setInterval(function, milliseconds
)
Same as setTimeout(), but repeats the execution of the function continuously.
Cookies
What are cookies? Cookies are data, stored in small text files, on your computer. Usually, the web server forgets everything about a visitor after the connection is shut down but cookies solve the problem of how details of the user can be stored and remembered when next the user visits the site. This in turn makes the site easy and faster to load because the webserver is already familiar or has bits of information of the user.
JavaScript Cookie Example
In the snippet to follow, we will create a cookie that stores the name of a visitor. The first time a visitor arrives at the web page, he/she will be asked to fill in his/her name. The name is then stored in a cookie. The next time the visitor arrives at the same page, he/she gets a welcome message.
The snippets include the following functions:
- A function to set a cookie value
- A function to get a cookie value
- A function to check a cookie value
Check out more details on today’s episode at w3schools.
What is this about?
30daysofjavascript is a series of writing on how I learn to code in JavaScript. These episodes are as simplified as possible and for beginners like me, I hope you find JavaScript less confusing throughout this episode. Thank you as always and see you in the next episode. Check out every episode I have written here.