The following are examples of Window Object.
Understanding Window Object
The Window Object gives you access to an even larger number or list of properties and methods than does the document object. By using the window object, you will be able to do a number of new things, such as promt the user for information, open new windows, confirm an action by a viewer and more. You will use this object quite often because of the properties and methods that it provides that enable you to interact with the viewer.
The window object is created for each window that appears on the screen. A window can be the main window, a frame set or individual frame, or even a new window created with JavaScript. This object makes available for use in your scripts a number of new properties and methods, some of which enable you to have more viewer interaction than in previous modules.
In previous modules we have used both the alert() method and the status property of the window object within our scripts. The alert() method enables us to pop up a message for the viewer, while the statud property enables us to change the value of the text string in the status bar at the bottom of the browser. You will now see a number of new methods of this object that do things such as confirm a viewer's action or prompt the viewer for information, and this will enable us to add more interactivity to our scripts.
Properties of the Window Object
When you opened this page an alert box opened asking what planet you are from. That was a prompt() method. The prompt() method is used to prompt the viewer to enter information. This allows you to add more interactivity with the viewer to your pages. You were also asked if you are sure that's where you are from. That is called a confirm() prompt. You had to say confirm or deny the place you entered in the first box. Nice galaxy we live in, right?
The following are going to show you different window object properties you can use in JavaScript directly in your document. First lets try a location property. The location property holds the current URL of the window and this URL can be changed on-the-fly to direct a viewer to a new page just like any link would. In the box below, type in your first name only and click! Use your back or return in your browser to come back here for more fun.
Next we will show you a status property. This is the value that is set in the status bar on the bottom of your browser. Currently is it set so say "I'm Just A Duck". By moving your mouse over the button below, you will see a new value in the status bar!
Next in the open() method, JavaScript allows you to open a new window. This method takes 3 parameters, the third which sets a number of options that the window may need. The first parameter URL sets the HTML document that will be opened. the name or second parameter is the name you wish to give to the window. The third enables you to add attributes for the new window.
So to try it out, click on the button below!
There is also a button on the new window called "Close This Window". That demonstrated the close() method. The close() method can only be used on a window that you have used signed scripts to open with JavaScript.
See, this JavaScript stuff isn't so bad, is it!