Document Object's Methods and History Object


© Maxim Karetnikov
Articles in this Topic    Discussions in this Topic

The document object provides five methods, through which you can manipulate its contents.

write string

Writes the string variable to the current document. For example:

Document.write "This is an example of the write method"

The string gets inserted in the current document at the current position, and it doesn't appear until the close method is called.

writeLn string

Places the string variable into the current document with a new-line character appended to the end. The new-line character is ignored by the browser, so the writeLn method operates practically the same as the write method.

open

Opens the document for output. The current contents of the document are cleared, and new strings can be placed in the document with the write and writeLn methods.

close

Updates the screen to display all of the strings written after the last open method call.

clear

Clears the contents of the document.

The common sequence of methods used to place data in the current document is

Document.open
Document.write "This is an example of write method"
Document.close

Instead of using a string enclosed in quotes, you can write a variable to the document, which could be the result of a calculation or a string that's calculated on the fly at the client's side (an expression that depends on the current day, for instance). The most interesting aspect of the write and writeLn methods is that their argument may also contain HTML tags. For instance, the statements

Document.open
Document.write "<h2 align="center">Title 1</h2>"
Document.write "<h3 align="center">Title 2</h3>"
Document.close

will cause title 1 and title 2 headings to appear and be centered on the browser's window.

The history object

The history object is an invisible object that provides methods for navigating through the document's history. It provides the functionality of the browser's navigational buttons, with the added benefit that you can access this functionality through programming. The methods of history object are as follows:

Back

Moves back in the history list by n steps, as if the user has clicked on the browser's Back button n times. The following statement returns you to the most recently visited URL:

window.history.back 1

Forward

Moves forward in the history list by n steps, as if the user has clicked on the browser's Forward button n times.

Go n

Moves to the nth item in the history list. The statement

window.history.go 1

will take you to the first URL in the list.

The history object also provides a property, the length property. This property represents the number of URLs in the history list.

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo


Here's the follow-up discussion on this article: View all related messages

1.   Apr 15, 2003 10:31 PM
if i use document.write method in event
previous content of document get erased
how can i print print without destroying previous contents ...

-- posted by deshmukhchetan





Join the latest discussions

For a complete listing of article comments, questions, and other discussions related to Maxim Karetnikov's VB Script topic, please visit the Discussions page.