Section 9.4. history, screen, and navigator


9.4. history, screen, and navigator

The remaining three objects that are direct children to the window object are history, screen, and the navigator. Between these three, you'll have a good idea of what kind of browser is accessing the page, and how much space you have in which to work. You'll also be able to send your web-page readers on their way using the history object.

As these objects are fairly simple in functionality and single-purposed, I'll review each, in turn, and then provide one example for all three at the end of this section.

9.4.1. history

The history object is just as it sounds: it maintains a history of pages loaded into the browser. As such, its methods and properties have to do with navigation through these pages, including going forward and back.

You can traverse through history using relational properties, such as next and previous, or using the methods back and forward. You can find the current page with current, and get the length of history (number of pages stored in the history cache). You can also go to a specific page using the go method and passing in a page numbernegative to go backward that many pages:

history.go(-3);

And positive to go forward:

history.go(3);

history, as they say, takes care of itself; you as page developer don't have to worry overmuch about it. About the only time when history becomes a concern is when using in-page techniques such as DHTML and Ajax, which work outside the normal patterns of page loading. However, we'll get into these issues later in the book. Returning to the BOM, the next object of interest accessible via the page hierarchy is the screen.

9.4.2. screen

The screen object contains information about the display screen, including width and height (both actual and available), as well as the color or pixel depth. Though not used very often, it is a good reference for any functionality that might change the size of the browser window or create colorful objects requiring a certain palette.

The exact properties supported can change from browser to browser, and version to version. At a minimum, most of the following are supported:


availTop (or top )

The topmost pixel position where a window can be positioned


availLeft (or left )

The leftmost pixel position where a window can be positioned


availWidth (or width )

Width of screen in pixels


availHeight (or height )

Height of screen in pixels


colorDepth

Color depth of the screen


pixelDepth

Bit depth of screen

The reason for the discrepancy between actual and available height and/or width is to accommodate the toolbar residing at the top, bottom, or side of many display screens.

In earlier DHTML implementations, developers would test the color depth of the screen and change to lower resolution images more appropriate to the configuration. However, even the more inexpensive monitors now support color depths greater than the older eight pixels, and most support true color. As such, the extra overhead to process the screen and return the images no longer has the payback it once had. Still, the color depth could alter your use of colors with style settings, so it's helpful informationas is the available width and height if you're working with a page layout.

9.4.3. navigator

Last, but not least, the navigator object provides information about the browser or other agent that accesses the page. This includes being able to check the operating system, the browser or browser family, security policy, language, and whether cookies are enabled. Some browsers also provide an array of installed plug-ins and other properties applicable to the specific user agent.

The navigator object usually supports the following:


appCodeName

The name of the browser code base


appName

The name of the browser


appMinorVersion

The minor version number (such as 52 for Version 1.52) of the browser


appVersion

The major version number (the 1.00 in 1.52) of the browser


cookieEnabled

Whether cookies are enabled


mimeTypes

An array of MIME types supported


onLine

Whether the user is online


platform

The platform on which the browser is operating


plugins

Array of plug-ins supported in browser


userAgent

Full agent description for browser (or other user agent)


userLanguage

Language supported in browser

The mimeTypes collection consists of mimeType objects, which have properties of description, type, and plugin. The plugins collection consists of plug-in objects with properties of a mimeType array of its own: description, filename, length of mimeType array, and plug-in name.

There are also a small number of methods that are supported among several browsers: javaEnabled, to test for Java enabling in the browser; preference to get and set browser preferences; and taintEnabled to check if data taint checking (a security feature) is enabled.

9.4.4. One Page, Three Objects

Example 9-11 is a page that runs through all three of the objects just coveredhistory, screen, and navigatorprinting out property values and providing a couple of options for testing history. Try it out in various browsers, in as many operating systems as you can, to see what's supported and what's not.

Example 9-11. Exploring the history, navigator, and screen objects

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>History,Screen,Navigator</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h1>history object</h1> <a href="" onclick="history.back(  );return false">history.back(  )</a><br /> <a href="" onclick="history.go(-2);return false">history.go(-2)</a><br /><br /> <a href="" onclick="history.forward(  );return false">history.forward(  )</a><br /> <h1>screen object</h1> <script type="text/javascript"> //<![CDATA[ document.writeln("screen.availTop: " + screen.availTop + "<br />"); document.writeln("screen.availLeft: " + screen.availLeft + "<br />"); document.writeln("screen.availWidth: " + screen.availWidth + "<br />"); document.writeln("screen.availHeight: " + screen.availHeight + "<br />"); document.writeln("screen.colorDepth: " + screen.colorDepth + "<br />"); document.writeln("screen.pixelDepth: " + screen.pixelDepth + "<br />"); document.writeln("<h1>navigator object</h1>"); document.writeln("navigator.userAgent: " + navigator.userAgent + "<br />"); document.writeln("navigator.appName: " + navigator.appName + "<br />"); document.writeln("navigator.appCodeName: " + navigator.appCodeName + "<br />"); document.writeln("navigator.appVersion: " + navigator.appVersion + "<br />"); document.writeln("navigator.appMinorVersion: " + navigator.appMinorVersion + "<br />"); document.writeln("navigator.platform: " + navigator.platform + "<br />"); document.writeln("navigator.cookieEnabled: " + navigator.cookieEnabled + "<br />"); document.writeln("navigator.onLine: " + navigator.onLine + "<br />"); document.writeln("navigator.userLanguage: " + navigator.userLanguage + "<br />"); document.writeln("navigator.mimeTypes[1].description: " + navigator.mimeTypes[1].description + "<br />"); document.writeln("navigator.mimeTypes[1].type: " + navigator.mimeTypes[1].type + "<br />"); document.writeln("navigator.plugins[3].description: " + navigator.plugins[3].description + "<br />"); //]]> </script> </body> </html>

You might be surprised at what shows up in some of the collections, such as the plugins. I know I was surprised to see one that provided digital-rights management, when I don't remember having installed a plug-in of this nature.

As for the mimeType object, some browsers also support a suffix property on the object, such as *.html and so on.

These three objects just demonstrated are the last of the objects directly accessible via the window object, save one. The last object covered in this chapter is an old friend by now: the document. In a way, most of the rest of the book focuses on the document object. However, we'll take a little time to look at it from a BOM perspective before moving into covering its role in DOM, DHTML, and Ajax.

The Cross-Browser MouseOver DOM Inspector

In earlier chapters I mentioned Firefox's DOM Inspector, which allows you to discover information about each element in the browser. There is a cross-browser-compatible version of this functionality, the MouseOver DOM Inspector (MODI) by slayeroffice.com, that works with Firefox, Mozilla, Netscape, Opera, and IE 6.x. It's a bookmark-based application; you can access it at http://slayeroffice.com/tools/modi/v2.0/modi_help.html.

Once bookmarked, when you're at a page and want to investigate the properties of all the page elements, just click the bookmark. A little in-page box opens that provides information about whatever element currently has cursor focus. When you want to stop inspecting the elements, just click the Esc key.

It is listed as beta software, but I found it worked nicely in all my browsers except Safari and the newer IE 7.x. Figure 9-2 shows it in use with Opera on the Mac at the O'Reilly web site.


Figure 9-2. Slayeroffice.com's MouseOver DOM Inspector


9.4.5. document

Returning to Figure 9-1 at the beginning of the chapter, you can see that the document object is what provides access to any element contained within the browser page. This includes forms and form elements, as well as cookies, all covered earlier. This also includes the collection of page images, links, embedded objects; in fact, all elements contained within the page boundaries have document as parent. Older variations of document had another collection, called layers, and the newer browser versions all share a style property, but the figure gives you an idea of how important the document is to dynamic page development.

The previous chapters have covered the document object methods of getElementById, as well as writeln; the next chapter on the DOM provides information on accessing all page elements using generic methods. For now, I want to pull back to the older method of accessing page elements through the various document collections, focusing on links, images, and the all-purpose all collection.

9.4.6. Links

The difference between a link and an anchor is the type of anchor attributes used. Both are based on the anchor tag (<a></a>). However, if an href attribute is provided, it's a link to another site; if just the name attribute is provided, it acts just as an anchor, which can set focus to a specific point in the page.

The links collection off of the document object consists of all hypertext links in the page, accessible as an array, starting with the first link in the page and moving down and to the right. However, you can also add an identifier for each hypertext link and access it in the array through this identifier.

Each item in the collection is a link object, which has properties of its own. Some properties are similar to those found with location: host, protocol, port, search, and hash, each of which returns that specific piece of the hypertext link. You can also access the complete link through the href property, and the associated linked object (text), tHRough text. This can be handy if you're pulling links from a document in a web page into a handy sidebar reference. Just make sure that you don't write the links out to the same page as the document, because you'll confuse the browser by adding new links at the same time as you're trying to process existing links.

In Example 9-12, the page contains text with three links. The links collection is accessed through the document object, and the links and associated text are extracted and printed just below the paragraphs.

Example 9-12. Pulling links from page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Reference</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <p>The <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/link.asp">links</a> collection off of the document <a href="http://www.w3.org/TR/html4/struct/objects.html">object</a> consists of all hypertext links in the page, accessible as an array, starting with the first link in the page and moving down and to the right. However, you can also add an identifier for each hypertext link and access it in the array through this identifier.</p><p>Each item in the collection is a <a href="http://www.devguru.com/Technologies/ecmascript/quickref/link.html">link</a> object, which has properties of its own. Among these are those similar to what we found with location: host, protocol, port, search, and hash, each of which returns that specific piece of the hypertext link. You can also access the complete link through the href property, and the associated linked object (text) through text. This can be handy if you're pulling links from a document in a web page, into a handy sidebar reference or other functionality such as this. </p> <h5>References</h5> <p> <script type="text/javascript"> //<![CDATA[ for (var i = 0; i < document.links.length; i++) {   var link = document.links[i];   document.writeln(link.text + " : " +  link.href + "<br />"); } //]]> </script> </p> </body> </html>

A better approach might be to provide alternative text in the link, using the title attribute, and then printing this out:

<a href="http://somelink.com" title="A better description of link">than this</a> ... document.writeln(link.title + " : " + link.href + "<br />");

However, this approach is sneaking into the higher-level DOMs where all attributes are accessible off an object. Still, regardless of level, most browsers support both.

9.4.7. Images

One of the earliest dynamic page-development techniques was to alter images within the document. This is still a popular technique for simple photo-slideshow types of applications, enabled through the document images collection.

As with links, images are objects in their own right, and you can set their attributessuch as src, the source URL for the imagedirectly. You can also create new instances of the images using the new constructor.

In Example 9-13, a slideshow is created of the first five images from Chapter 1, and a simple mechanism is put in place to traverse the list, replacing the current document image with the next one in the list. An array of images preloads the images when the page loads so that the transition happens more quickly.

Example 9-13. Creating a slideshow using the images collection

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Slideshow</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ var currentPhoto = 0; var pics = new Array(  ); for (var i = 0; i < 5; i++) {   pics[i] = new Image(  ); } pics[0].src = "fig01-1.jpg"; pics[1].src = "fig01-2.jpg"; pics[2].src = "fig01-3.jpg"; pics[3].src = "fig01-4.jpg"; pics[4].src = "fig01-5.jpg"; function changePhoto(photo) {    document.images[0].src = pics[photo].src; } function nextPic(  ) {   currentPhoto++;   if (currentPhoto < pics.length) {     changePhoto(currentPhoto);   } else {     alert("at the end of the photo list");   } } function prevPic(  ) {   if (currentPhoto > 0) {     currentPhoto--;     changePhoto(currentPhoto);   } else {     alert("at the beginning of the photo list");   } } //]]> </script> <img src="/books/4/327/1/html/2/fig01-1.jpg" /> <p> <a href="" onclick="nextPic(  );return false">Next picture</a> <a href="" onclick="prevPic(  ); return false">Previous picture</a> <p> </head> </body> </html>

Again, like the previous example with links, this example tends to blur the line between DOM levels. However, it also works in all of the most popular web browsers, which is what's important for our purposes.

Also notice in Example 9-13 that, along with the images, the src attribute can be changed. This differs from Example 9-12, which just outputs the link attributes. The image source is an attribute that can be read or written, while the link attributes can only be read. There are ways, though, to adjust all page elements, and we'll look at them in the next section.




Learning JavaScript
Learning JavaScript, 2nd Edition
ISBN: 0596521871
EAN: 2147483647
Year: 2006
Pages: 151

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net