04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8: The Browser Object Model<br />

The end result is that there ’ s no way to determine the size of the browser window itself, but it is possible<br />

to get the dimensions of the page viewport as shown in the following example:<br />

var pageWidth = window.innerWidth,<br />

pageHeight = window.innerHeight;<br />

if (typeof pageWidth != “number”){<br />

if (document.compatMode == “CSS1Compat”){<br />

pageWidth = document.documentElement.clientWidth;<br />

pageHeight = document.documentElement.clientHeight;<br />

} else {<br />

pageWidth = document.body.clientWidth;<br />

pageHeight = document.body.clientHeight;<br />

}<br />

}<br />

In this code, pageWidth and pageHeight are assigned initial values of window.innerWidth and window<br />

.innerHeight , respectively. A check is then done to see if the value of pageWidth is a number; if not, then<br />

the code determines if the page is in standards mode by using document.compatMode (this property is<br />

discussed fully in Chapter 10 ). If it is, then document.documentElement.clientWidth and document<br />

.documentElement.clientHeight are used; otherwise, document.body.clientWidth and<br />

document.body.clientHeight are used.<br />

The browser window can be resized using the resizeTo() and resizeBy() methods. Each method<br />

accepts two arguments: resizeTo() expects a new width and height, and resizeBy() expects the<br />

differences in each dimension. Here ’ s an example:<br />

//resize to 100 x 100<br />

window.resizeTo(100, 100);<br />

//resize to 200 x 150<br />

window.resizeBy(100, 50);<br />

//resize to 300 x 300<br />

window.resizeTo(300, 300);<br />

As with the window - movement methods, the resize methods may be disabled by the browser and are<br />

disabled by default on Opera and IE 7 and later. Also like the movement methods, these methods apply<br />

only to the topmost window object.<br />

Navigating and Opening Windows<br />

The window.open() method can be used both to navigate to a particular URL and to open a new<br />

browser window. This method accepts four arguments: the URL to load, the window target, a string of<br />

features, and a Boolean value indicating that the new page should take the place of the currently loaded<br />

page in the browser history. Typically only the first three arguments are used; the last argument applies<br />

only when not opening a new window.<br />

If the second argument passed to window.open() is the name of a window or frame that already exists,<br />

then the URL is loaded into the window or frame with that name. Here ’ s an example:<br />

//same as < a href=”http://www.wrox.com” target=”topFrame” > < /a ><br />

window.open(“http://www.wrox.com/”, “topFrame”);<br />

207

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!