This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
15.6 JS Popup Alert
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.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
window.alert("sometext");
The window.alert() method can be written without the window prefix.
alert("I am an alert box!");
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" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax
window.confirm("sometext");
The window.confirm() method can be written without the window prefix.
if (confirm("Press a button!")) {
txt = "You pressed OK!";
} else {
txt = "You pressed Cancel!";
}
Prompt Box
A prompt box is often 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.
Syntax
window.prompt("sometext","defaultText");
The window.prompt() method can be written without the window prefix.
var person = prompt("Please enter your name", "Harry Potter");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
} else {
txt = "Hello " + person + "! How are you today?";
}
Line Breaks
To display line breaks inside a popup box, use a back-slash followed by the character n.
alert("Hello\nHow are you?");
Module 15. JS Browser BOM
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Window - Window Size
JS Screen - Window Screen Width
JS Screen - Window Screen Height
JS Screen - Window Screen Available Width
JS Screen - Window Screen Available Height
JS Window Screen - Color Depth
JS Window Screen - Pixel Depth
JS Location - Window Location Href
JS Location - Window Location Hostname
JS Location - Window Location Pathname
JS Location - Window Location Protocol
JS Location - Window Location Port
JS Location - Window Location Assign
JS Navigator Object - Browser Cookies
JS Navigator Object - Browser Application Name
JS Navigator Object - Browser Application Code Name
JS Navigator Object - The Browser Engine
JS Navigator Object - The Browser Version
JS Navigator Object - The Browser Agent
JS Navigator Object - The Browser Platform
JS Navigator Object - The Browser Language
JS Navigator Object - Is The Browser Online?
JS Navigator Object - Is Java Enabled?
JS Alert - Alert Box
JS Alert - Confirm Box
JS Alert - Prompt Box
JS Alert - Line Breaks
JS Timing - The setTimeout() Method
JS Timing - How to Stop the Execution? part 1
JS Timing - The setInterval() Method
JS Timing - How to Stop the Execution? part 2
JS Cookies example