Window Methods and Properties

 

Introduction.  Regardless of what they are called in a particular programming language or on a particular operating system, everyone works with windows.  For example, using JavaScript while interacting with a browser, 
  • you can view the results of an event in a new window
  • you can interact with a site that automatically closes your browser window when you logoff
  • you can stack the open windows with a particular window on top
  • you can size windows
  • you can put messages in the status bar

There are many other things that can be done with windows in a browser using JavaScript.  The following table will list a fairly large variety of the properties one can make use of when interacting with windows.  The object and syntax used to invoke these properties is window.property.

 

Property Description
closed

this is a read only Boolean that specifies whether a window has been closed

defaultStatus

a string that specifies the default message that appears in a window's status bar

document a reference to the document object contained in a window
frames[ ] an array of the frames contained in a window
history a reference to the history object for this window
length frames.length gives the number of elements in the frames[ ] array
location a reference to the location object for the window
Math an object containing mathematical functions and constants
name a string that contains the name of the window
navigator we used this previously
opener this returns the name of the object that called the open( ) method used to invoke this window
parent a reference to the parent window or frame of the current window
screen a reference to the screen object for the window
self a reference to the window itself, essentially the same as window
status a string that specifies the current contents of the status bar
top a reference to the top level window that contains the current window.  This is only really useful with frames.
window a reference to the window itself, essentially the same as self

 

As should always be the case in OOP, when there are properties, there are methods and the Windows object in JavaScript has both.  The following is a list of many of the methods associated with Window objects.  The object and syntax used to invoke these properties is window.method( ).

 

Method Descritpion Syntax
alert( )

displays a simple message in a dialog box

alert(message)

blur( )

take keyboard focus away from the top level browser window, usually sending it to the background

 
close( ) close a window  
confirm( ) a yes or no question with a dialog box confirm(question)
focus( ) give the top level browser window keyboard focus, this usually brings the window to the front  
moveBy( ) move the window by a relative amount moveBy(dx, dy)
moveTo( ) move the window to an absolute position moveTo(x, y)
open( ) create and open a new window open(url, name, features, replace)
prompt( ) ask for a simple string input with a dialog box prompt(message, default)
resizeBy( ) resize the window by a specified amount resizeBy(dw, dh)
resizeTo( ) resize the window to a specified size resizeTo(width, height)
scroll( ) scroll the document displayed in the window scroll(x, y)
scrollBy( ) scroll the window by a specified amount scrollBy(dx, dy)
scrollTo( ) scroll the window to a specified position scrollTo(x, y)
setInterval( ) execute the code at specified periodic time intervals setInterval(code, interval)
setTimeout( ) execute the code after a specified amount of time elapses setTimeout(code, delay)

 

Now we need to work some examples to make use of these properties and methods.