List of Methods of document Object in JavaScript

Leave a Comment

In this article, we have list out methods of document objects in JavaScript.

captureEvents

The captureEvents method instructs the document to capture and handle all events of a particular type.

Syntax
document.captureEvents(eventType)

open

The output stream is opened by document.open() method. This is opened to collect the output of subsequent document.write() or document.writeln methods.

Sysntax
document.open(mimetype, replace)

close

The close method of a document object is used for closing the output stream previously opened with the document.open method. This also collects and displays the collected data in this process.

Example
<html>
<head>
<script type=”text/javascript”>
Function exfor(){
var opendocu = document.open(“text/html”, “replace”);
var test = “<html><body>Welcome to Exforsys!</body></html>”;
exfor.write(test);
exfor.close();
}
</script>
</head>
<body>
<input type=”button” value=”Click Here!” onclick=”exfor()”>
</body>
</html>

getElementById

In the getElementById method, the object id is specified in argument getElementById() and this returns the reference to the first object with the specified ID.

Syntax
document.getElementById(id)

Example
<html>
<head>
<script type="text/javascript">
function ShiftDoll(){
 dollvar = document.getElementById('doll').style;
 dollvar.left = 500;
}
</script>
</head>
<body>
<p>
<a href="#" onmousedown="ShiftDoll()">Click for Dancing Doll</a>
</p>
<p>
<img src="doll.jpg" id="doll" style="position:absolute;width:520px;height400px;" />
</p>
</body>
</html>

getElementsByName

The getElementsById method was used for returning the reference to the first object with the specified ID. The programmer can also get a collection of objects with the object name given in argument by using the method getElementsByName.

The getElementsByName() method returns a collection of objects with the specified Name given in argument. This method returns an array of elements with a name attribute whose value matches that of the parameter given.

Syntax
document.getElementsByName(name)

Example
<html>
<head>
<script type="text/javascript">
function Exforinput(){
 var test = document.getElementsByName("Exforsys");
 alert(test.length);
}
</script>
</head>
<body>
<input name="Exforsys" type="text" size="30" /><br>
<input name="Exforsys" type="text" size="30" /><br>
<input type="button" onClick="Exforinput()" value="Click to see the number of input box" /><br>
</body>
</html>

getSelection

A programmer can return the contents of a selected text in the current document by using the getSelection() method.

Syntax 
document.getSelection()

Example
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<input type="button" onClick="alert('You have selected the text:\n'+document.getSelection());" value="Selected Highlighted text is displayed!!" /><br>
</body>
</html>

handleEvent

For the handleEvent method the event is specified as a parameter and the method calls the corresponding handler for the specified event. When a call of the handleEvent method is made, a search for the corresponding available set of event handler function is performed. This is then passed to the event for an appropriately mapped handler function.

The general syntax for using the handleEvent method of document object is as follows:
document.handleEvent(event)

releaseEvents

The releaseEvents method is used for indicating the events that are no longer needed. This is captured by the receiving Document object. The functionality is similar to that of window.releaseEvents() method.

Syntax
document.releaseEvents(eventType)

routeEvent

The routeEvent() method passes all previously captured events of the event type passed through their normal event processes.

Syntax
document.routeEvent(event)

write

If a programmer wishes to write HTML expressions to the specified document, then it can be done using write method of document object.

Syntax
document.write(exp1, exp2, …)

writeln

The functionality of writeln method of document object is similar to that of write method but here in writeln method places a carriage return after the written value of each expression.

Syantax
document.writeln(exp1, exp2, ….)

Example
<html>
<head>
<script type="text/javascript">
document.writeln("Welcome to StudyOverflow.Org");
</script>
</head>
<body>
</body>
</html>

0 comments:

Post a Comment