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 11: DOM Levels 2 and 3<br />

offsetParent<br />

border<br />

padding<br />

content<br />

clientHeight<br />

clientWidth<br />

Figure 11-2<br />

The client dimensions are literally the amount of space inside of the element, so the space taken up by<br />

scrollbars is not counted. The most common use of these properties is to determine the browser viewport<br />

size, as discussed in Chapter 8 . This is done by using the clientWidth and clientHeight of document<br />

.documentElement or document.body (in IE versions prior to 7), as shown in the following example:<br />

function getViewport(){<br />

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

return {<br />

width: document.body.clientWidth,<br />

height: document.body.clientHeight<br />

};<br />

} else {<br />

return {<br />

width: document.documentElement.clientWidth,<br />

height: document.documentElement.clientHeight<br />

};<br />

}<br />

}<br />

This function determines whether or not the browser is running in quirks mode by checking the document<br />

.compatMode property. Safari prior to version 3.1 doesn ’ t support this property, so it will automatically<br />

continue execution in the else statement. Chrome, Opera, and Firefox run in standards mode most of the<br />

time, so they will also continue to the else statement. The function returns an object with two properties:<br />

width and height . These represent the dimensions of the viewport (the < html > or < body > elements).<br />

As with offset dimensions, client dimensions are read - only and are calculated each<br />

time they are accessed.<br />

338

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

Saved successfully!

Ooh no, something went wrong!