Window

The Window interface represents a the browser's window. A global variable, window, representing the window in which the script is running, is exposed to JavaScript code.

When a variable or function not previously defined is used, this means that the property or method of the window is being used.

 window.alert("Oh, no!");
// same as
alert("Oh, no!");

properties

Most properties are read only.

property description
document Returns a reference to the document that the window contains.
fullscreen Indicates whether the window is displayed in full screen mode or not.
if (window.fullScreen) {
  // it's fullscreen!
}
else {
  // not fullscreen!
}
history Reference to the History object, which provides an interface for manipulating the browser session history (pages visited in the tab or frame that the current page is loaded in).
location Reference to the Location object with information about the current location of the document.
// show current location
alert(location); 
// forcelly reload page
location.reload(true);
// change current address
location.href="http://socode4.com"
navigator Reference to the Navigator object, that contains information about the visitor's browser.
frames Array of frames. Each item represents the window object corresponding to the given frame's or iframe's content. In other words window.frames[0] is the same thing as document.getElementsByTagName("iframe")[0].contentWindow.
parent Reference to the parent of the current window or subframe. If a window does not have a parent, its parent property is a reference to itself.
top The topmost window in the hierarchy of window objects. This property is especially useful when you are dealing with a window that is in a subframe of a parent or parents, and you want to get to the top-level frameset.
status The text in the statusbar at the bottom of the browser.
name Name of the window.
innerWidth The width of the content area of the browser window including, if rendered, the vertical scrollbar.
innerHeight The height of the content area of the browser window including, if rendered, the horizontal scrollbar.

methods

methods description
open(url, windowName, [windowFeatures]) Loads the specified resource into the browsing context (window, frame or tab) with the specified name. If the name doesn't exist, then a new window is opened and the specified resource is loaded into its browsing context.
  • url - URL of the resource to be loaded. This can be a path or URL to an HTML page, image file, or any other resource which is supported by the browser. If the empty string ("") is specified as url, a blank page is opened into the targeted browsing context.
  • windowName - name of the browsing context. This name can be used as the target for the target attribute of a or form elements. The name should not contain whitespace. This will not be used as the window's title.
  • windowFeatures - a DOMString containing a comma-separated list of window features given with their corresponding values in the form "name=value". These features include options such as the window's default size and position, whether or not to include scroll bars, and so forth. There must be no whitespace in the string.
close() Closes the current window, or the window on which it was called.
alert(msg) Displays an alert dialog with the optional specified content and an OK button.
  • msg - optional message
print() Opens the Print Dialog to print the current document.
promt(message,
default)
Displays a dialog with an optional message prompting the user to input some text.
  • message - text to display to the user. Can be omitted if there is nothing to show in the prompt window.
  • default - a string containing the default value displayed in the text input field
var result = window.prompt(message, default);
confirm(message) Displays a modal dialog with an optional message and two buttons: OK and Cancel.
  • message - optional string to display in the dialog.
Return true if user selected Ok button.
moveBy(deltaX,
deltaY)
Moves the current window by a specified amount.
  • deltaX - amount of pixels to move the window horizontally. Positive values are to the right, while negative values are to the left.
  • deltaY - is the amount of pixels to move the window vertically. Positive values are down, while negative values are up.
moveTo(x,y) Moves the current window to the specified coordinates.
  • x - the new horizontal coordinate
  • y - the new vertical coordinate
resizeBy(dx, dy) Resizes the current window by a specified amount.
  • dx - number of pixels to grow the window horizontally
  • dy - number of pixels to grow the window vertically
resizeTo(w,h) Resizes the window.
  • w - the new width in pixels
  • h - the new height in pixels
scroll(x,y) Scrolls the window to a particular place in the document.
  • x - x coordinate along the horizontal axis of the document that you want displayed in the upper left
  • y - y coordinate along the vertical axis of the document that you want displayed in the upper left
scroll(options) Scrolls the window to a particular place in the document.
window.scroll({
  top: 100,
  left: 100,
  behavior: 'smooth'
});
scrollBy(dx, dy) Scrolls the document in the window by the given amount.
  • dx - horizontal offset in pixels
  • dy - vertical offset in pixels
scrollBy(options) Scrolls the document in the window by the given amount.
window.scrollBy({
  top: 100,
  left: 100,
  behavior: 'smooth'
});