Introduction
In this article, we will understand the “window” object in the JavaScript language. In JavaScript, most of the objects are created on top of the window object. Actually the “window” object represents the browser window and many properties of it. In this article, we will understand a few of them with a practical approach.
The window object is supported by all browsers. It represents the browser's window.
All global JavaScript objects, functions, and variables automatically become members of the window object.
Global variables are properties of the window object.
Even the document object (of the HTML DOM) is a property of the window object.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- alert(window.document.write("writtten in window"));
- </script>
-
- </form>
-
- </body>
- </html>
Now let us attach a variable to the window object.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
-
- <script>
- var name = "sagar";
-
- function xyz() {
- alert("value of name is:- " + window.name);
- }
-
-
- </script>
- <input type="button" value="value" name="value" onclick="xyz()" />
- </form>
-
- </body>
- </html>
When we navigate from page to page in a web application, all the history is stored in the history object. We can get the length of the history object created on top of the “window” object, as shown in the following:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- alert("Length of history:- " + window.history.length);
- </script>
-
- </form>
-
- </body>
- </html>
Summary
In this article, we learned about the window object in JavaScript. In a future article, we will learn more basic concepts of JavaScript.
Reference