| |
Creating Objects
You can create your own objects. Creating your own object requires two steps:
- Defining the object type.
- Create an instance of the object with new.
User-defined objects are also associated with properties and methods. To define an object type, create a function for the object type that specifies its name, along with its properties and methods. Objects may be created using the following syntax:
| Syntax |
| name = new Object ( ) |
For instance Array, Date, Number, Boolean, and String objects can be created this way as in the example below. Most objects may be created this way except for the Math object which cannot be instantiated (i.e, an instance of it may not be created).
var Rollno. = new array (1,3,6,7,10)
var Student=new String( "name" )
var Birdate = new date( )
String objects may also be created as follows:
var string = "I am coming."
Document Object
The document object provides access to the properties that relate to the current HTML document, such as title, form name, document colors, links etc. Document object is the container for all HEAD and BODY objects associated within the HTML tags of a web page. This object also offers you to change the contents of the document dynamically.
Properties of Document Object
Property |
Description |
alinkColor |
This property defines the color of active links defined as a hexadecimal triplet or as a string. |
anchors |
It refers to an array containing references to the anchor objects contained in an HTML document. |
applets |
It defines an array containing references to all the applet objects contained in the current document. |
bgColor |
It sets background color of the document. It is set in the <BODY> tag. It is defined as a hexadecimal triplet or as a string. |
cookie |
It is string, used to identify the value of a cookie associated with the specified document. |
domain |
It sets or returns the domain name of the document server; Used for the security purposes. |
embeds |
It defines an array containing all the plug-in objects contained in the current document. |
fgColor |
This property sets the text color( foreground ) of the document. |
forms |
It defines an array containing references to all the form objects contained in the current document. |
images |
It defines an array of images in the current document. |
lastModified |
It returns the date, the file was last modified. |
layers |
It is an array containing all the layer objects contained in the current document. |
linkColor |
This property specifies the color of any hyperlinks in the document. The color is defined as hexadecimal triplet or as a string. It is specified in the <BODY> tag. |
title |
This property returns the name of the current document. It is specified between the header <TITLE> tag. |
URL |
A string that is used to retrieve the location of the current document. |
vlinkColor |
It defines the color of visited links in the document. It is specified in the <BODY> tag. The color is defined as a hexadecimal triplet or as a string. |
| |
|
Methods of Document Object
| Property |
Description |
| alinkColor |
This property defines the color of active links defined as a hexadecimal triplet or as a string. |
| anchors |
It refers to an array containing references to the anchor objects contained in an HTML document. |
| applets |
It defines an array containing references to all the applet objects contained in the current document. |
| bgColor |
It sets background color of the document. It is set in the <BODY> tag. It is defined as a hexadecimal triplet or as a string. |
| cookie |
It is string, used to identify the value of a cookie associated with the specified document. |
| domain |
It sets or returns the domain name of the document server; Used for the security purposes. |
| embeds |
It defines an array containing all the plug-in objects contained in the current document. |
| fgColor |
This property sets the text color( foreground ) of the document. |
| forms |
It defines an array containing references to all the form objects contained in the current document. |
| images |
It defines an array of images in the current document. |
| lastModified |
It returns the date, the file was last modified. |
| layers |
It is an array containing all the layer objects contained in the current document. |
| linkColor |
This property specifies the color of any hyperlinks in the document. The color is defined as hexadecimal triplet or as a string. It is specified in the <BODY> tag. |
| title |
This property returns the name of the current document. It is specified between the header <TITLE> tag. |
| URL |
A string that is used to retrieve the location of the current document. |
| vlinkColor |
It defines the color of visited links in the document. It is specified in the <BODY> tag. The color is defined as a hexadecimal triplet or as a string. |
Methods of Document object
| Methods |
Description |
| clear ( ) |
This method clears all elements in the document. |
| close ( ) |
It closes an output stream that was used to create a document object and displays the data sent. |
| createAttribute( ) |
This method creates an attribute with the name, specified. |
| captureEvents( ) |
It specifies that a document will capture and handle all the events of a specific type. |
| contextual( ) |
It specifies the style followed by specific tags. |
| focus( ) |
The document is given focus. |
| getElementById( ) |
This method returns a reference to the first object with particular id. With this method you can access any element on the page via its ID attribute. |
| getElementsByName( ) |
This method returns objects with the particular name. |
| getSelection( ) |
It returns the contents of selected text, if any, in the current document. |
| open( ) |
It opens a stream used to create a new document object for writing. |
| releaseEvents( ) |
It sets the current document to release any events of a particular type, which can then be passed to other objects further down to the event hierarchy. |
| routeEvent( ) |
It sends a particular event through the standard event hierarchy. |
| write( ) |
This method is used to write text string and HTML expressions to the specified document |
| writeln( ) |
This method also writes the text string and HTML expressions to the specified document, but it also adds a new line character. |
Events of Document Object
| Events |
Description |
| onClick |
It fires some predefined JavaScript code, when an element is clicked. |
| onDblClick |
It fires some specified JavaScript code, when an element is double clicked. |
| onFocus |
It executes specified JavaScript code, when an element receives the current input focus. |
| onKeyDown |
This event executes some specified JavaScript code, when user presses a key. |
| onKeyPress |
This event executes some specified JavaScript code, when user presses a key and held down. |
| onKeyUp |
This event executes some predefined JavaScript code, when a particular key is released. |
| onMouseDown |
This event executes specified code, when the user presses a mouse button. |
| onMouseMove |
This event executes specified code, when the user moves the mouse. |
| onMouseOver |
This event executes some specified code, when the mouse is moved over an element. |
| onMouseUp |
This event executes some specified code, when the user releases a mouse button. |
| onMouseOut |
This event executes some specified JavaScript code, when the mouse is moved out of an element. |
| onResize |
This event executes some specified JavaScript code, when the user resizes a window. |
|