How to use Javascript Window Object
Methods from Window Object
The windows object has the following properties and methods:
Name | Description | Returns |
---|---|---|
alert(msg) | Displays a message dialog window. | void |
blur() | Unfocuses the window. | void |
clearInterval(id) | Cancels an interval timer. | void |
clearTimeout(id) | Cancels a timeout timer. | void |
close() | Closes the window. | void |
confirm(msg) | Displays a OK/Cancel prompt dialog. | boolean |
defaultView | Returns the Window for the active document. | Window |
document | Returns the Document object associated with this window. | Document |
focus() | Focuses the window. | void |
frames | Returns an array of the Window objects for the iframe elements. | Window[] |
history | Returns the browser history. | History |
innerHeight | Gets the height of the window content area. | number |
innerWidth | Gets the width window content area. | number |
length | Returns the number of nested iframe elements in the document. | number |
location | Returns the current document's location. | Location |
opener | Returns the Window that opened the current browsing context. | Window |
outerHeight | Gets the height of the window, including borders, menu bars, etc. | number |
outerWidth | Gets the width of the window, including borders, menu bars, etc. | number |
pageXOffet | Gets the number of pixels that the window has been scrolled horizontally from the topleft corner. | number |
pageYOffset | Gets the number of pixels that the window has been scrolled vertically from the top-left corner. | number |
parent | Returns the parent of the current Window. | Window |
postMessage(msg, origin) | Sends the message to another document. | void |
print() | Prompts the user to print the page. | void |
prompt(msg, val) | Displays a dialog prompting the user to enter a value. | string |
screen | Returns a Screen object describing the screen. | Screen |
screenLeft/screenX | Gets the number of pixels from the left edge of the window to the left edge of the screen. | number |
screenTop/screenY | Gets the number of pixels from the top edge of the window to the top edge of the screen. | number |
scrollBy(x, y) | Scrolls the document relative to its current position. | void |
scrollTo(x, y) | Scrolls to the specified position. | void |
self | Returns the Window for the current document. | Window |
setInterval(function, time) | Creates a timer that will call the specified function every time milliseconds. | int |
setTimeout(function, time) | Creates a timer that will call the specified function once after time milliseconds. | int |
showModalDialog( | Displays a pop-up window showing the specified URL. | void |
stop() | Stops the document loading. | void |
top | Returns the top-most Window. | Window |
Window Events
The window
object defines a wide range of events in the following table.
Name | Description |
---|---|
onabort | Triggered when the loading of a document or resource is aborted. |
onafterprint | Triggered when the Window.print() method is called, before the user is presented with the print options. |
onbeforeprint | Triggered after the user has printed the document. |
onerror | Triggered when there is an error loading a document or resource. |
onhashchange | Triggered when the hash fragment changes. |
onload | Triggered when the loading of a document or resource is complete. |
onpopstate | Triggered to provide a state object associated with the browser history. |
onresize | Triggered when the window is resized. |
onunload | Triggered when the document is unloaded from the window/browser. |