JavaScript DOM /BOM/ ES6
Browser Object Model |- BOM, in JavaScript
The important methods of window object are as follows:
| Method | Description |
|---|---|
| alert() | displays the alert box containing message with ok button. |
| confirm() | displays the confirm dialog box containing message with ok and cancel button. |
| prompt() | displays a dialog box to get input from the user. |
| open() | opens the new window. |
| close() | closes the current window. |
| setTimeout() | performs action after specified time like calling function, evaluating expressions etc. |
alert()
<html lang="en">
<head>
<script>
function msg(){
alert("Hello Alert Box");
}
</script>
</head>
<body>
<input type="button" value="click" onclick="msg()"/>
</body>
</html>
confirm() in javascript
It displays the confirm dialog box. It has message with ok and cancel buttons.
<html lang="en">
<head>
<script>
function msg(){
var v= confirm("Are u sure?");
if(v==true){
alert("ok");
}
else{
alert("cancel");
}
}
</script>
</head>
<body>
<input type="button" value="delete record" onclick="msg()"/>
</body>
</html>
prompt() in javascript
It displays prompt dialog box for input. It has message and text field.
<html lang="en">
<head>
<script>
function msg(){
var v= prompt("Who are you?");
alert("I am "+v);
}
</script>
</head>
<body>
<input type="button" value="click" onclick="msg()"/>
</body>
</html>
open() in javascript
It displays the content in a new window.
<html lang="en">
<head>
<script>
function msg(){
open("http://www.codeguruva.blogspot.com");
}
</script>
</head>
<body>
<input type="button" value="javatpoint" onclick="msg()"/>
</body>
</html>
setTimeout() in javascript
Definition and Usage
The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.
Tip: 1000 ms = 1 second.
Tip: The function is only executed once. If you need to repeat execution, use the setInterval() method.
Tip: Use the clearTimeout() method to prevent the function from running.
Example
Display an alert box after 2 seconds (2000 milliseconds):
It performs its task after the given milliseconds.
<html lang="en">
<head>
<script>
function msg(){
setTimeout(
function(){
alert("Welcome to Javatpoint after 2 seconds")
},2000);
}
</script>
</head>
<body>
<input type="button" value="click" onclick="msg()"/>
</body>
</html>