JavaScript Bible, Gold Edition part 53 pot

10 154 0
JavaScript Bible, Gold Edition part 53 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

368 Part III ✦ Document Objects Reference Table 18-3 NN6 document.implementation.hasFeature() Support Feature Versions XML 1.0, 2.0 HTML 1.0, 2.0 Views 2.0 StyleSheets 2.0 CSS 2.0 Events 2.0 MouseEvents 2.0 HTMLEvents 2.0 Range 2.0 Example on the CD lastModified Value: Date String Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓ ✓✓ ✓ ✓ ✓✓✓ Every disk file maintains a modified timestamp, and most (but not all) servers are configured to expose this information to a browser accessing a file. This infor- mation is available by reading the document.lastModified property. If your server supplies this information to the client, you can use the value of this property to present this information for readers of your Web page. The script automatically updates the value for you, rather than requiring you to hand-code the HTML line every time you modify the home page. If the value returned to you displays itself as a date in 1969, it means that you are positioned somewhere west of GMT, or Greenwich Mean Time (some number of time zones west of GMT at 1 January 1970), and the server is not providing the proper data when it serves the file. Sometimes server configuration can fix the problem, but not always. The returned value is not a date object (Chapter 36) but rather a straight string consisting of time and date, as recorded by the document’s file system. The format of the string varies from browser to browser and version to version. You can, how- ever, usually convert the date string to a JavaScript date object and use the date On the CD-ROM document.lastModified 369 Chapter 18 ✦ The Document and Body Objects object’s methods to extract selected elements for recompilation into readable form. Listing 18-6 shows an example. Some browser versions running in Windows 95 may return a two-digit year, which will lead to Y2K problems when generating a date object. Even local file systems don’t necessarily provide the correct data for every browser to interpret. For example, in Navigator of all generations for the Macintosh, dates from files stored on local disks come back as something from the 1920s (although Internet Explorer manages to reflect the correct date). But put that same file on a UNIX or NT Web server, and the date appears correctly when accessed via the Net. Example on the CD with Listing 18-6 Related Items: Date object (Chapter 36). layers Value: Array Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓ The layer object (Chapter 31) is the NN4 way of exposing positioned elements to the object model. Thus, the document.layers property is an array of positioned elements in the document. But due to the nonstandard way that NN4 implements positioned elements, not every positioned element is represented in the document.layers array. More deeply nested positioned elements must be referenced through a hierarchy of layers. The layer object and document.layers property are orphaned in NN4, and their importance diminishes as the installed base of NN4 shrinks. The remaining dis- cussion is included only for those Web authors who must support positioned ele- ments in NN4. In NN6, the layer is represented by any HTML element whose style sheet definition includes a position attribute. References to such elements can be made through the document.getElementById() method or shortcuts described in Chapter 14. A Netscape layer is a container for content that can be precisely positioned on the page. Layers can be defined with the NN4-specific <LAYER> tag or with W3C standard style sheet positioning syntax, as explained in Chapter 31. Each layer con- tains a document object — the true holder of the content displayed in that layer. Note On the CD-ROM Note document.layers 370 Part III ✦ Document Objects Reference Layers can be nested within each other, but a reference to document.layers reveals only the first level of layers defined in the document. Consider the following HTML skeleton. <HTML> <BODY> <LAYER NAME=”Europe”> <LAYER NAME=”Germany”></LAYER> <LAYER NAME=”Netherlands”></LAYER> </LAYER> </BODY> </HTML> From the point of view of the primary document, there is one layer (Europe). Therefore, the length of the document.layers array is 1. But the Europe layer has a document, in which two more layers are nested. A reference to the array of those nested layers is document.layers[1].document.layers or document.Europe.document.layers The length of this nested array is two: The Germany and Netherlands layers. No property exists that reveals the entire set of nested arrays in a document, but you can create a for loop to crawl through all nested layers (shown in Listing 18-7). Example on the CD with Listing 18-7 Related Items: layer object (Chapter 31). linkColor See alinkColor. links Value: Array Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓ ✓✓ ✓ ✓ ✓✓✓ The document.links property is similar to the document.anchors property, except that the objects maintained by the array are link objects — items created with <A HREF=””> tags. Use the array references to pinpoint a specific link for retrieving any link property, such as the target window specified in the link’s HTML definition. On the CD-ROM document.links 371 Chapter 18 ✦ The Document and Body Objects Link arrays begin their index counts with 0: The first link in a document has the reference document.links[0]. And, as with any array object, you can find out how many entries the array has by checking the length property. For example: var linkCount = document.links.length Entries in the document.links property are full-fledged location objects. Example on the CD Related Items: link object; document.anchors property. location URL Value: String Read/Write and Read-Only (see text) NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility (✓) ✓✓✓(✓)(✓) ✓✓✓ The fact that JavaScript frequently reuses the same terms in different contexts may be confusing to the language’s newcomers. Such is the case with the document.location property. You may wonder how this property differs from the location object (Chapter 17). In practice, many scripts also get the two confused when references don’t include the window object. As a result, a new property name, document.URL, was introduced in NN3 and IE4 to take the place of document.location. You can still use document.location, but the term may eventually disappear from the object model vocabulary. To help you get into the future mindset, the rest of this discussion refers to this property as document.URL. The remaining question is how the window.location object and document.URL property differ. The answer lies in their respective data types. A location object, you may recall from Chapter 17, consists of a number of properties about the document currently loaded in a window or frame. Assigning a new URL to the location object (or location.href property) tells the browser to load the page from that URL into the frame. The document.URL property, on the other hand, is simply a string (read-only in Navigator) that reveals the URL of the current document. The value may be important to your script, but the property does not have the “object power” of the window.location object. You cannot change (assign another value to) this property value because a document has only one URL: its location on the Net (or your hard disk) where the file exists, and what protocol is required to get it. This may seem like a fine distinction, and it is. The reference you use ( window. location object or document.URL property) depends on what you are trying to accomplish specifically with the script. If the script is changing the content of a window by loading a new URL, you have no choice but to assign a value to the On the CD-ROM document.location 372 Part III ✦ Document Objects Reference window.location object. Similarly, if the script is concerned with the component parts of a URL, the properties of the location object provide the simplest avenue to that information. To retrieve the URL of a document in string form (whether it is in the current window or in another frame), you can use either the document.URL property or the window.location.href property. Example on the CD with Listings 18-8, 18-9, and 18-10 Related Items: location object; location.href, URLUnencoded properties. media Value: String Read/Write NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓ At its introduction in IE5.5, the document.media property is limited to one value besides the default value of empty: print. Details of this property are sketchy, but the intention appears to be to provide a way to use scripting to set the equivalent of the CSS2 @media rule (one of the so-called “at” rules because of the at symbol). This style sheet rule allows browsers to assign separate styles for each type of output device on which the page is rendered (for example, perhaps a different font for a printer versus the screen). In practice, however, this property is not modifiable in IE5.5. Related Items: None. mimeType Value: String Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓ Although this property is readable in IE5+, its value is not strictly speaking a MIME type, or at least not in traditional MIME format. Moreover, the results are inconsistent between IE5 and IE5.5. Perhaps this property will be of more use in an XML, rather than HTML, document environment. In any case, this property in no way exposes supported MIME types in the current browser. On the CD-ROM document.mimeType 373 Chapter 18 ✦ The Document and Body Objects namespaces Value: Array of namespace objects Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓ A namespace object (new in IE5.5) can dynamically import an XML-based IE Element Behavior. The namespaces property returns an array of all namespace objects defined in the current document. For more details on how to utilize Element Behaviors and ViewLinks (custom controls devised out of HTML and scripting) in IE5.5, visit http://msdn.microsoft.com/workshop/author/behaviors/ overview/identityb_ovw.asp . Related Items: None. parentWindow Value: window object reference Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓✓ The document.parentWindow property returns a reference to the window object containing the current document. The value is the same as any reference to the cur- rent window. Example on the CD Related Items: window object. plugins Value: Array Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓ ✓✓✓ The document.plugins property returns the same array of EMBED element objects that you get from the document.embeds property. This property appears to have been deprecated in favor of document.embeds. Related Items: document.embeds property. On the CD-ROM document.plugins 374 Part III ✦ Document Objects Reference protocol Value: String Read/Write NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓✓ The IE-specific document.protocol property returns the plain-language version of the protocol that was used to access the current document. For example, if the file is accessed from a Web server, the property returns Hypertext Transfer Protocol . This property differs from the location.protocol property, which returns the portion of the URL that includes the often more cryptic protocol abbre- viation (for example, http:). As a general rule, you want to hide all of this stuff from a Web application user. Example on the CD Related Items: location.protocol property. referrer Value: String Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓ ✓✓ ✓ ✓ ✓✓✓ When a link from one document leads to another, the second document can, under JavaScript control, reveal the URL of the document containing the link. The document.referrer property contains a string of that URL. This feature can be a useful tool for customizing the content of pages based on the previous location the user was visiting within your site. A referrer contains a value only when the user reaches the current page via a link. Any other method of navigation (such as through the history, bookmarks, or by manually entering a URL) sets this property to an empty string. The document.referrer property is broken in Windows versions of IE3 and IE4. In the Windows version, the current document’s URL is given as the referrer; the proper value is returned in the Macintosh versions. For IE5+, the property returns empty when the referrer document is accessed via the file: protocol. Example on the CD with Listings 18-11 and 18-12 Related Items: link object. On the CD-ROM Note On the CD-ROM document.referrer 375 Chapter 18 ✦ The Document and Body Objects scripts Value: Array Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓✓ The IE-specific document.scripts property returns an array of all SCRIPT ele- ment objects in the current document. You can reference an individual SCRIPT ele- ment object to read not only the properties it shares with all HTML element objects (Chapter 15) but also script-specific properties, such as defer, src, and htmlFor. The actual scripting is accessible either through the innerText or text properties for any SCRIPT element object. While the document.scripts array is read-only, many properties of individual SCRIPT element objects are modifiable. Adding or removing SCRIPT elements impacts the length of the document.scripts array. Don’t forget, too, that if your scripts need to access a specific SCRIPT element object, you can assign an ID attribute to it and reference the element directly. This property is an IE-specific convenience property that is the same as the IE4+ and NN6 expression document.getElementsByTagName(“SCRIPT”), which returns an array of the same objects. Example on the CD Related Items: SCRIPT element object (Chapter 20). security Value: String Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓ The security property reveals information about a security certificate, if one is associated with the current document. As of this writing, the property is not for- mally documented by Microsoft, so its range of possibilities is not clear for now. For a standard document, the value of the property is This type of document does not have a security certificate. On the CD-ROM document.security 376 Part III ✦ Document Objects Reference selection Value: Object Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓✓ The document.selection property returns a selection object whose content is represented in the browser window as a body text selection. That selection can be explicitly performed by the user (by clicking and dragging across some text) or created under script contol via the IE/Windows TextRange object (see Chapter 19). Because script action on a selection (for example, finding the next instance of selected text) is performed via the TextRange object, converting a selection to a TextRange object using the document.selection.createRange() method is common practice. See the selection object in Chapter 19 for more details. Be aware that you cannot script interaction with text selections through user interface elements, such as buttons. Clicking a button gives focus to the button and deselects the selection. Use other events, such as document.onmouseup to trigger actions on a selection. Example on the CD Related Items: selection, TextRange objects. styleSheets Value: Array Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓✓✓ The document.styleSheets array consists of references to all STYLE element objects in the document. Not included in this array are style sheets that are assigned to elements by way of the STYLE attribute inside a tag or linked in via LINK elements. See Chapter 30 for details about the styleSheet object. Related Items: styleSheet object (Chapter 30). On the CD-ROM document.styleSheets 377 Chapter 18 ✦ The Document and Body Objects tags Value: Array Read-Only NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓ The NN4-specific tags property is used in the browser’s alternate, JavaScript- based style sheet syntax. Deployment of JavaScript style sheets is exceedingly rare. In some ways, the document.tags property behaves like the IE4+ and NN5 document.getElementsByTagName() method, but document.tags cannot be used in regular scripts to access element objects. Related Items: ids property. title Value: String Read-Only and Read/Write NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓ ✓✓ ✓ ✓ ✓✓✓ A document’s title is the text that appears between the <TITLE> </TITLE> tag pair in an HTML document’s Head portion. The title usually appears in the title bar of the browser window in a single-frame presentation. Only the title of the top- most framesetting document appears as the title of a multiframe window. Even so, the title property for an individual document within a frame is available via scripting. For example, if two frames are available ( UpperFrame and LowerFrame), a script in the document occupying the LowerFrame frame can reference the title property of the other frame’s document, such as this: parent.UpperFrame.document.title This property is read-only in browsers prior to IE4 and NN6. The document.title property is a holdover from the original document object model. HTML elements in recent browsers have an entirely different application of the title property (see Chapter 15). In IE4+ and NN6, you should address the doc- ument’s title by way of the TITLE element object directly. UNIX versions of Navigator 2 fail to return the document.title property value. Also, in Navigator 4 for the Macintosh, if a script creates the content of another frame, the document.title property for that dynamically written frame returns the filename of the script that wrote the HTML, even when it writes a valid <TITLE> tag set. Related Items: history object. Note document.title . On the CD-ROM document.location 372 Part III ✦ Document Objects Reference window.location object. Similarly, if the script is concerned with the component parts of a URL, the properties of the. ✓ The NN4-specific tags property is used in the browser’s alternate, JavaScript- based style sheet syntax. Deployment of JavaScript style sheets is exceedingly rare. In some ways, the document.tags. true holder of the content displayed in that layer. Note On the CD-ROM Note document.layers 370 Part III ✦ Document Objects Reference Layers can be nested within each other, but a reference to

Ngày đăng: 06/07/2014, 06:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan