Difference between revisions of "JavaScript Document Object"
(→Document Object Properties) |
(→Writing text to a document) |
||
Line 115: | Line 115: | ||
This is a line of text specified in the HTML file | This is a line of text specified in the HTML file | ||
This is a line of text written to the document using JavaScript | This is a line of text written to the document using JavaScript | ||
+ | </pre> | ||
+ | |||
+ | === Changing the Document Title === | ||
+ | |||
+ | Even when the title of a document has been specified using the HTML ''<title>'' tag it is possible to dynamically change the current setting using the ''title'' property of the document object. The following example intially sets the document title to ''JavaScript Document Object Example'' and provides a button which, when pressed, change sthe title to a new value: | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | <html> | ||
+ | <head> | ||
+ | <title>JavaScript Document Object Example</title> | ||
+ | </head> | ||
+ | <body> | ||
+ | <p> | ||
+ | This is a line of text specified in the HTML file | ||
+ | </p> | ||
+ | <form action=""> | ||
+ | <input type=button value="Press to change title" onclick="document.title='hello'"/> | ||
+ | </form> | ||
+ | </body> | ||
+ | </html> | ||
+ | |||
</pre> | </pre> |
Revision as of 20:31, 14 May 2007
In the JavaScript Window Object chapter we looked at how perform such tasks as opening and closing browser windows and controlling the appearance and position of those windows. Whilst the window object gives us the ability to control various aspects of the browser window it does not provide us any control over the content of the window. In order to manipulate the content that appears in the browser we need to turn to the JavaScript Document object.
What is the JavaScript Document Object?
The JavaScript Document object essentially contains the HTML elements that are contained the <head> and <body> sections of a web page. It provides the hooks (via a set of object methods and properties) that enable basic changes to be made to the content of a browser window from within a JavaScript script.
The Document object gets the initial value of its title property from the HTML <title> tag and a number of color and event values from the <body> element:
<body [alink="activated link color"] [background="background image file"] [bgcolor="background color"] [link="unvisited link color"] [onload="page load functionName"] [onunload="page unload functionName"] [text="foreground color"] [vlink="visited link color"]> </body>
It is important to note that these are only optional initial values. Regardless of whether these are declared in the <body> element they can still be set, or modified from JavaScript via the Document object.
JavaScript Document Object Methods and Properties
The JavaScript Document contains a wide range of methods and properties that are considerable use to developers. The following table lists the key methods and properties provided by the Document object:
Document Object Methods
Method | Description |
---|---|
open() | Opens the output stream to the document for writing |
close() | Closes the output stream to the document to prevent further writing |
write() | Appends text to the document. |
writeln() | Appends text to the document followed by a newline character |
Document Object Properties
Property | Description |
---|---|
alinkcolor | The color to be used when displaying activated links in the document |
anchors | An array of anchor objects present in the document |
applets | An array of Java Applet objects embedded into the document |
bgColor | The background color of the document |
fgColor | The foreground color (i.e. the color of the text in the document) |
cookie | The cookie, if aany, assosiated with the document. See Understanding JavaScript Cookies |
domain | The current domain of the document |
embeds | An array of objects embedded into the document |
forms | An array of the Forms objects contained in the document. See JavaScript Forms and HTML |
images | An Array of Image objects contained the document |
lastModified | The date that the document was last modified |
linkColor | The color to be used when displaying links in the document |
links | An array of links contained in the document |
referrer | The URL of the document from which the current document was linked (i.e. how the visitor arrived at this page) |
title | The title of the docuemnt (corresponds to the HTML <title> tag) |
URL | The URL of the current document |
vlinkColor | The color to be used when displaying links in the document which have been visited by the current user |
Using the JavaScript Document Object
Now that we have seen the methods and properties that are avialable with the Document object we can begin explore how to use them:
Writing text to a document
The document object open() and write() methods can be used to dynamically write text to a web page. The following example contains both some text contained in the HTML file, togetehr with some JavaScript to write additional text into the page:
<html> <head> <title>JavaScript Document Object Example</title> </head> <body> <p> This is a line of text specified in the HTML file </p> <script language="javascript" type="text/javascript"> document.open() document.write("This is a line of text written to the document using JavaScript"); </script> </body> </html>
The above example will display the following in the browser window:
This is a line of text specified in the HTML file This is a line of text written to the document using JavaScript
Changing the Document Title
Even when the title of a document has been specified using the HTML <title> tag it is possible to dynamically change the current setting using the title property of the document object. The following example intially sets the document title to JavaScript Document Object Example and provides a button which, when pressed, change sthe title to a new value:
<html> <head> <title>JavaScript Document Object Example</title> </head> <body> <p> This is a line of text specified in the HTML file </p> <form action=""> <input type=button value="Press to change title" onclick="document.title='hello'"/> </form> </body> </html>