JavaScript DOM Window Object

1. Window Object

  • Window object is a top-level object in Client-Side JavaScript.
  • Window object represents the browser's window.
  • It represents an open window in a browser.
  • It supports all browsers.
  • The document object is a property of the window object. So, typing window.document.write is same as document.write.
  • All global variables are properties and functions are methods of the window object.
Window Object Properties

PropertyDescription
DocumentIt returns the document object for the window (DOM).
FramesIt returns an array of all the frames including iframes in the current window.
ClosedIt returns the boolean value indicating whether a window has been closed or not.
HistoryIt returns the history object for the window.
innerHeightIt sets or returns the inner height of a window's content area.
innerWidthIt sets or returns the inner width of a window's content area.
LengthIt returns the number of frames in a window.
LocationIt returns the location object for the window.
NameIt sets or returns the name of a window.
NavigatorIt returns the navigator object for the window.
OpenerIt returns a reference to the window that created the window.
outerHeightIt sets or returns the outer height of a window, including toolbars/scrollbars.
outerWidthIt sets or returns the outer width of a window, including toolbars/scrollbars.
ParentIt returns the parent window of the current window.
ScreenIt returns the screen object for the window.
screenXIt returns the X coordinate of the window relative to the screen.
screenYIt returns the Y coordinate of the window relative to the screen.
SelfIt returns the current window.
StatusIt sets the text in the status bar of a window.
TopIt returns the topmost browser window that contains frames.

Window Object Method

MethodDescription
alert()It displays an alert box.
confirm()It displays a dialog box.
prompt()It displays a dialog box that prompts the visitor for input.
setInterval()It calls a function or evaluates an expression at specified intervals.
setTimeout()It evaluates an expression after a specified number of milliseconds.
clearInterval()It clears a timer specified by setInterval().
clearTimeOut()It clears a timer specified by setTimeout().
close()It closes the current window.
open()It opens a new browser window.
createPopup()It creates a pop-up window.
focus()It sets focus to the current window.
blurt()It removes focus from the current window.
moveBy()It moves a window relative to its current position.
moveTo()It moves a window to the specified position.
resizeBy()It resizes the window by the specified pixels.
resizeTo()It resizes the window to the specified width and height.
print()It prints the content of the current window.
scrollBy()It scrolls the content by the specified number of pixels.
scrollTo()It scrolls the content to the specified coordinates.

Example : Simple Program on Window Object

open_window.html          //File name

<html>
<head>
     <script type="text/javascript">
     function openwindow()
     {
          window.open("welcome.html");
     }
     </script>
</head>
<body>
     <form>
          <input type="button" value="Open" onClick=window.alert()>
          <input type="button" onClick="openwindow()" value="Open Window">
     </form>
</body>
</html>

welcome.html          //File name

<html>
     <body>
     <script type="text/javascript">
     {
          document.write("<b>Welcome to TutorialRide !!!</b>");
     }
     </script>
     </body>
</html>


Output:

window object 1


window object 2

  • In the above JavaScript program, when you click on the 'Open Window', you will see the 'welcome.html' opened in another window.
  • When you click on the 'Open' button, you will see the alert message box.

2. Navigator Object

  • Navigator object is the representation of Internet browser.
  • It contains all the information about the visitor's (client) browser.
Navigator Object Properties

PropertyDescription
appNameIt returns the name of the browser.
appCodeNameIt returns the code name of the browser.
appVersionIt returns the version information of the browser.
cookieEnabledIt determines whether cookies are enabled in the browser.
platformIt returns which platform the browser is compiled.
userAgentIt returns the user agent header sent by the browser to the server.

Navigator Object Methods

MethodDescription
javaEnabled()It specifies whether or not the browser is Java enabled.

Example : Simple Program on Navigator Object

<html>
     <body>
     <script type="text/javascript">
          document.write("<b>Browser: </b>"+navigator.appName+"<br><br>");
          document.write("<b>Browser Version: </b>"+navigator.appVersion+"<br><br>");
          document.write("<b>Browser Code: </b>"+navigator.appCodeName+"<br><br>");
          document.write("<b>Platform: </b>"+navigator.platform+"<br><br>");
          document.write("<b>Cookie Enabled: </b>"+navigator.cookieEnabled+"<br><br>");
          document.write("<b>User Agent: </b>"+navigator.userAgent+"<br><br>");
          document.write("<b>Java Enabled: </b>"+navigator.javaEnabled()+"<br><br>");
     </script>
     </body>
</html>


Output:

Browser: Netscape

Browser Version: 5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36

Browser Code: Mozilla

Platform: Linux x86_64

Cookie Enabled: true

User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36

Java Enabled: true

3. History Object

  • History object is a part of the window object.
  • It is accessed through the window.history property.
  • It contains the information of the URLs visited by the user within a browser window.
History Object Properties

PropertyDescription
LengthIt returns the number of URLs in the history list.
CurrentIt returns the current document URL.
NextIt returns the URL of the next document in the History object.
PreviousIt returns the URL of the last document in the history object.

History Object Methods

MethodDescription
back()It loads the previous URL in the history list.
forward()It loads the next URL in the history list.
go(“URL”)It loads a specific URL from the history list.

4. Location Object

  • Location object is a part of the window object.
  • It is accessed through the 'window.location' property.
  • It contains the information about the current URL.
Location Object Properties

PropertyDescription
hashIt returns the anchor portion of a URL.
hostIt returns the hostname and port of a URL.
hostnameIt returns the hostname of a URL.
hrefIt returns the entire URL.
pathnameIt returns the path name of a URL.
portIt returns the port number the server uses for a URL.
protocolIt returns the protocol of a URL.
searchIt returns the query portion of a URL.

Location Object Methods

MethodDescription
assign()It loads a new document.
reload()It reloads the current document.
replace()It replaces the current document with a new one.


Example : Simple Program on Location Object

<html>
     <body>
     <script type="text/javascript">
          document.write("<b>Path Name: </b>"+location.pathname+"<br><br>");
          document.write("<b>Href: </b>"+location.href+"<br><br>");
          document.write("<b>Protocol: </b>"+location.protcol+"<br><br>");
     </script>
     </body>
</html>


Output:

Path Name: /home/tutorialride2/location_object.html

Href: file:///home/tutorialride2/location_object.html

Protocol: undefined