What is a window object in Javascript ?
Window object is the object of the browser and not a javascript object.
Window object represents the window in the browser.
Window object is created automatically by the browser.
How to access the window object ?
Window objects can be executed with or without the ‘window’ prefix.
Syntax :
window.screen
or
screen
Javascript complete tutorial !!!
Click to Learn More about – Javascript online learning
What are the methods of window object ?
There are few methods of window object are as follows:
1. alert()
alert() method displays the alert box containing a message with ok button.
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
alert("HI Good Morning, Have a nice day");
</script>
</head>
<body></body>
</html>
Output :
![](https://s30.e0b.myftpupload.com/wp-content/uploads/2021/11/javascript-28-example1-cc-1024x300.png)
2. confirm()
confirm() method displays the confirm dialog box containing message with ok and cancel button.
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
confirm("Do you wish to continue ?");
</script>
</head>
<body></body>
</html>
Output :
![](https://s30.e0b.myftpupload.com/wp-content/uploads/2021/11/javascript-28-example2-cc-1024x320.png)
3. prompt()
prompt() method displays a dialog box to get input from the user.
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
prompt("Do you wish to continue ?");
</script>
</head>
<body></body>
</html>
Output :
![](https://s30.e0b.myftpupload.com/wp-content/uploads/2021/11/javascript-28-example3-cc-1024x351.png)
4. open()
open() method opens the new window.
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
open("https://google.com");
</script>
</head>
<body></body>
</html>
Output :
A new window with (https://google.com) is loaded.
5. close()
close() method closes the current window.
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
close();
</script>
</head>
<body></body>
</html>
Output :
This closes the current window.
6. setTimeout()
setTimeout() method performs action after specified time like calling function, evaluating expressions etc.
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
setTimeout(
function(){
alert("Hi, Welcome after 2 seconds")
},2000);
</script>
</head>
<body></body>
</html>
Output :
![](https://s30.e0b.myftpupload.com/wp-content/uploads/2021/11/javascript-28-example4-cc-1024x324.png)