Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Accessing the Browser

Another important object built into JavaScript is the window object. The window object represents the browser and provides you with a wealth of information about the browser position, size, and much more. It also allows you to open new child windows, close windows, and even resize the window.

Getting the Max Viewable Web Page Size

The window object provides the innerHeight and innerWidth of the browser window. These values represent the actual pixels in the browser window that the web page will be displayed within. This is a critical piece of information if you need to adjust the size and location of elements on the web page based on the actual area that is being displayed.

Setting the Text Displayed in the Browser Status Bar

The browser has a status bar at the bottom. You can set the text that is displayed there to provide to the user additional information that does not belong on the page, such as the server name, current status of requests, and more. To set the text displayed in the browser status bar, set the window.status value equal to the string you want displayed.

Getting the Current Location in the Web Page

When writing dynamic code, it is often necessary to determine the exact location in the web page that is currently being viewed. When the user scrolls down or to the right, the position of the page to the frame of the browser view port changes.

To determine the number of pixels the page has scrolled to the right, use the window.pageXOffset attribute. To determine the number of pixels the page has scrolled down, use the window.pageYOffset attribute.

Opening and Closing Windows

The window object also provides a set of methods that allow you to create and manage additional child windows from your JavaScript code.

For example, the window.open(URL) method opens a new window and returns a new window object. If you do not specify a URL, the browser opens a blank page that can be written to using the window.document object.

You can call .close() on window objects that you have created, and they will be closed.

  • + Share This
  • 🔖 Save To Your Account