1. Trang chủ
  2. » Công Nghệ Thông Tin

JavaScript Bible, Gold Edition part 53 pot

10 155 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 149,36 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Table 18-3 NN6 document.implementation.hasFeature SupportExample on the CD lastModified Value: Date String Read-Only Every disk file maintains a modified timestamp, and most but not all

Trang 1

Table 18-3 NN6 document.implementation.hasFeature() Support

Example on the CD

lastModified

Value: Date String Read-Only

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.lastModifiedproperty 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

Trang 2

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:Dateobject (Chapter 36)

layers

The layer object (Chapter 31) is the NN4 way of exposing positioned elements to

the object model Thus, the document.layersproperty 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.layersarray 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 documentobject — the true holder of the content displayed in that layer

Note

On the

CD-ROM

Note

document.layers

Trang 3

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.layersarray 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 forloop 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

The document.linksproperty is similar to the document.anchorsproperty, 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

Trang 4

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 lengthproperty For example:

var linkCount = document.links.length

Entries in the document.linksproperty are full-fledged locationobjects

Example on the CD

Related Items: link object; document.anchorsproperty

location

URL

Value: String Read/Write and Read-Only (see text)

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.locationproperty You may wonder how this property differs from the

locationobject (Chapter 17) In practice, many scripts also get the two confused

when references don’t include the windowobject 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.locationobject and document.URL

property differ The answer lies in their respective data types

A locationobject, 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 locationobject (or location.hrefproperty) tells the browser to

load the page from that URL into the frame The document.URLproperty, 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.locationobject 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

locationobject or document.URLproperty) 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

Trang 5

window.locationobject Similarly, if the script is concerned with the component parts of a URL, the properties of the locationobject 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.hrefproperty

Example on the CD with Listings 18-8, 18-9, and 18-10

Related Items:locationobject; location.href, URLUnencodedproperties

media

Value: String Read/Write

At its introduction in IE5.5, the document.mediaproperty 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 @mediarule (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

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

Trang 6

Value: Array of namespace objects Read-Only

A namespaceobject (new in IE5.5) can dynamically import an XML-based IE

Element Behavior The namespacesproperty 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:windowobject reference Read-Only

The document.parentWindowproperty returns a reference to the windowobject

containing the current document The value is the same as any reference to the

cur-rent window

Example on the CD

Related Items:windowobject

plugins

The document.pluginsproperty returns the same array of EMBED element

objects that you get from the document.embedsproperty This property appears to

have been deprecated in favor of document.embeds

Related Items:document.embedsproperty

On the

CD-ROM

document.plugins

Trang 7

Value: String Read/Write

The IE-specific document.protocolproperty 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.protocolproperty, 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

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.referrerproperty 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

Trang 8

The IE-specific document.scriptsproperty returns an array of all SCRIPT

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 eleele-ment objects

(Chapter 15) but also script-specific properties, such as defer, src, and htmlFor

The actual scripting is accessible either through the innerTextor textproperties

for any SCRIPT element object

While the document.scriptsarray is read-only, many properties of individual

SCRIPT elementobjects are modifiable Adding or removing SCRIPT elements

impacts the length of the document.scriptsarray 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

The securityproperty 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

Trang 9

Value: Object Read-Only

The document.selectionproperty returns a selectionobject 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 TextRangeobject (see Chapter 19) Because script action on a selection (for example, finding the next instance of selected text) is performed via the TextRangeobject, converting a selection to a

TextRangeobject using the document.selection.createRange()method is common practice See the selectionobject 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.onmouseupto trigger actions on a selection

Example on the CD

Related Items:selection, TextRangeobjects

styleSheets

The document.styleSheetsarray 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 STYLEattribute inside a tag or linked in via LINK elements See Chapter 30 for details about the styleSheetobject

Related Items:styleSheetobject (Chapter 30)

On the

CD-ROM

document.styleSheets

Trang 10

The NN4-specific tagsproperty 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.tagsproperty behaves like the IE4+ and NN5

document.getElementsByTagName()method, but document.tagscannot be

used in regular scripts to access element objects

Related Items:idsproperty

title

Value: String Read-Only and Read/Write

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 titleproperty for an individual document within a frame is available via

scripting For example, if two frames are available (UpperFrameand LowerFrame),

a script in the document occupying the LowerFrameframe 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.titleproperty is a holdover from the original document object

model HTML elements in recent browsers have an entirely different application of

the titleproperty (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:historyobject

Note

document.title

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

TỪ KHÓA LIÊN QUAN