javascript - window object
The window
property of a window object points to the window object itself. Thus the following expressions all return the same window object:
window.window window.window.window window.window.window.window ...
In web pages, the window object is also a global object. This means that:
- global variables of your script are in fact properties of
window
:var global = {data: 0}; alert(global === window.global); // displays "true"
- you can access built-in properties of the window object without having to type
window.
prefix:setTimeout("alert('Hi!')", 50); // equivalent to using window.setTimeout. alert(window === window.window); // displays "true"
The point of having the window
property refer to the object itself was (probably) to make it easy to refer to the global object (otherwise you'd have to do a manual var window = this;
assignment at the top of your script).
Another reason is that without this property you wouldn't be able to write, for example, "window.open('http://google.com/')
" - you'd have to just use "open('http://google.com/')" instead.
Yet another reason to use this property is for libraries which wish to offer OOP-versions and non-OOP versions (especially JavaScript modules). If, for example, we refer to "this.window.location.href", a JavaScript module could define a property called "window" inside of a class it defined (since no global "window" variable exists for it by default) which, could be created, for example, after passing in a window object to the module class' constructor. Thus, "this.window" inside of its functions would refer to that window object. In the non-namespaced version, "this.window" would simply refer back to "window", and also be able to get the document location without trouble. Another advantage is that the objects of such a class (even if the class were defined outside of a module) could change their reference to the window at will, as they would not be able to do if they had hard-coded a reference to "window" (yet the default in the class could still be set as the current window object).
Specification
source - https://developer.mozilla.org/ko/docs/Web/API/Window/window
JavaScript Window - The Browser Object Model
The Browser Object Model (BOM) allows JavaScript to "talk to" the browser.
The Browser Object Model (BOM)
There are no official standards for the Browser Object Model (BOM).
Since modern browsers have implemented (almost) the same methods and properties for JavaScript interactivity, it is often referred to, as methods and properties of the BOM.
The Window Object
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.
Global functions are methods of the window object.
Even the document object (of the HTML DOM) is a property of the window object:
is the same as:
Window Size
Three different properties can be used to determine the size of the browser window (the browser viewport, NOT including toolbars and scrollbars).
For Internet Explorer, Chrome, Firefox, Opera, and Safari:
- window.innerHeight - the inner height of the browser window
- window.innerWidth - the inner width of the browser window
For Internet Explorer 8, 7, 6, 5:
- document.documentElement.clientHeight
- document.documentElement.clientWidth
- or
- document.body.clientHeight
- document.body.clientWidth
A practical JavaScript solution (covering all browsers):
Example
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
Try it Yourself »
The example displays the browser window's height and width: (NOT including toolbars/scrollbars)
Other Window Methods
Some other methods:
- window.open() - open a new window
- window.close() - close the current window
- window.moveTo() -move the current window
- window.resizeTo() -resize the current window
source - http://www.w3schools.com/js/js_window.asp
Window Object
The window object represents an open window in a browser.
If a document contain frames (<iframe> tags), the browser creates one window object for the HTML document, and one additional window object for each frame.
Note: There is no public standard that applies to the Window object, but all major browsers support it.
Window Object Properties
Property | Description |
---|---|
closed | Returns a Boolean value indicating whether a window has been closed or not |
defaultStatus | Sets or returns the default text in the statusbar of a window |
document | Returns the Document object for the window (See Document object) |
frameElement | Returns the <iframe> element in which the current window is inserted |
frames | Returns all <iframe> elements in the current window |
history | Returns the History object for the window (See History object) |
innerHeight | Returns the inner height of a window's content area |
innerWidth | Returns the inner width of a window's content area |
length | Returns the number of <iframe> elements in the current window |
location | Returns the Location object for the window (See Location object) |
name | Sets or returns the name of a window |
navigator | Returns the Navigator object for the window (See Navigator object) |
opener | Returns a reference to the window that created the window |
outerHeight | Returns the outer height of a window, including toolbars/scrollbars |
outerWidth | Returns the outer width of a window, including toolbars/scrollbars |
pageXOffset | Returns the pixels the current document has been scrolled (horizontally) from the upper left corner of the window |
pageYOffset | Returns the pixels the current document has been scrolled (vertically) from the upper left corner of the window |
parent | Returns the parent window of the current window |
screen | Returns the Screen object for the window (See Screen object) |
screenLeft | Returns the horizontal coordinate of the window relative to the screen |
screenTop | Returns the vertical coordinate of the window relative to the screen |
screenX | Returns the horizontal coordinate of the window relative to the screen |
screenY | Returns the vertical coordinate of the window relative to the screen |
scrollX | An alias of pageXOffset |
scrollY | An alias of pageYOffset |
self | Returns the current window |
status | Sets or returns the text in the statusbar of a window |
top | Returns the topmost browser window |
Window Object Methods
Method | Description |
---|---|
alert() | Displays an alert box with a message and an OK button |
atob() | Decodes a base-64 encoded string |
blur() | Removes focus from the current window |
btoa() | Encodes a string in base-64 |
clearInterval() | Clears a timer set with setInterval() |
clearTimeout() | Clears a timer set with setTimeout() |
close() | Closes the current window |
confirm() | Displays a dialog box with a message and an OK and a Cancel button |
createPopup() | Creates a pop-up window |
focus() | Sets focus to the current window |
moveBy() | Moves a window relative to its current position |
moveTo() | Moves a window to the specified position |
open() | Opens a new browser window |
print() | Prints the content of the current window |
prompt() | Displays a dialog box that prompts the visitor for input |
resizeBy() | Resizes the window by the specified pixels |
resizeTo() | Resizes the window to the specified width and height |
scroll() | Deprecated. This method has been replaced by the scrollTo() method. |
scrollBy() | Scrolls the document by the specified number of pixels |
scrollTo() | Scrolls the document to the specified coordinates |
setInterval() | Calls a function or evaluates an expression at specified intervals (in milliseconds) |
setTimeout() | Calls a function or evaluates an expression after a specified number of milliseconds |
stop() | Stops the window from loading |
source - http://www.w3schools.com/jsref/obj_window.asp