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 />

Whenever frames are used, multiple Global objects exist in the browser. Global<br />

variables defined in each frame are defined to be properties of that frame ’ s window<br />

object. Since each window object contains the native type constructors, each frame<br />

has its own version of the constructors, which are not equal. For example,<br />

top.Object is not equal to top.frames[0].Object ,which affects the use of<br />

instanceof when objects are passed across frames.<br />

Window Position<br />

The position of a window object may be determined and changed using various properties and methods.<br />

Internet Explorer (IE), Safari, Opera, and Chrome all provide screenLeft and screenTop properties<br />

that indicate the window ’ s location in relation to the left and top of the screen, respectively. Firefox<br />

provides this functionality through the screenX and screenY properties, which are also supported in<br />

Safari and Chrome. Opera supports screenX and screenY , but you should avoid using them in Opera,<br />

because they don ’ t correspond to screenLeft and screenTop . The following code determines the left<br />

and top positions of the window across browsers:<br />

var leftPos = (typeof window.screenLeft == “number”) ?<br />

window.screenLeft : window.screenX;<br />

var topPos = (typeof window.screenTop == “number”) ?<br />

window.screenTop : window.screenY;<br />

This example uses the ternary operator to determine if the screenLeft and screenTop properties exist.<br />

If they do (which is the case in IE, Safari, Opera, and Chrome), they are used. If they don ’ t exist (as in<br />

Firefox), screenX and screenY are used.<br />

There are some quirks to using these values. In IE, Opera, and Chrome, screenLeft and screenTop<br />

refer to the space from the left and top of the screen to the page view area represented by window . If the<br />

window object is the topmost object and the browser window is at the very top of the screen (with a<br />

y - coordinate of 0), the screenTop value will be the pixel height of the browser toolbars that appear<br />

above the page view area. Firefox and Safari treat these coordinates as being related to the entire browser<br />

window, so placing the window at y - coordinate 0 on the screen returns a top position of 0.<br />

To further confuse things, Firefox, Safari, and Chrome always return the values of top.screenX and<br />

top.screenY for every frame on the page. Even if a page is offset by some margins, these same values<br />

are returned every time screenX and screenY are used in relation to a window object. IE and Opera<br />

give accurate coordinates for the location of frames in relation to the screen edges.<br />

205

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

Saved successfully!

Ooh no, something went wrong!