Exforsys

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction

Ads


Home arrow Technical Training arrow JavaScript Tutorial

JavaScript Document Object Methods Part I

Author : Exforsys Inc.     Published on: 5th Aug 2007

JavaScript Document Object Methods - Part I

In this JavaScript tutorial, you will learn about methods of document object along with syntax and examples.  This section covers captureEvents, open, close, getElementById, getElementsByName methods with explanations for each method.

Ads

Methods of document Object:

captureEvents:

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

The general syntax for using the captureEvents method of document object is as follows:

document.captureEvents(eventType)

The list of eventType in an earlier section of this tutorial called Event Handler in JavaScript.

For example:

document.captureEvents(Event.MOUSEMOVE);

In the above, the MouseMove event is captured. When the MouseMove event occurs, then the event handler function defined in document.onMouseMove executes.

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.

The general syntax for using the open method of document object is as follows:

document.open(mimetype,replace)

In the above syntax, both mimetype and replace are optional. The mimetype refers to the type of document the programmer wants to write to. The default value of this is "text/html". When replace is mentioned, it causes the history entry for the new document to inherit the history entry from the parent document.

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.

The output stream is opened by document.open() method. This is opened to collect the output of subsequent document.write() or document.writeln methods. Once all the writes are performed, the document.close() method causes any output written to the output stream to be displayed in the document opened in a secondary window.

The general syntax for using the close method of document object is as follows:

document.close()

Let us see an example to understand the concept of open and close method in detail:


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

The output of the above program is a button with message

Click Here!

and on clicking this the output appears in a new document exfor as

Welcome to Exforsys!

The document exfor closes after this statement exfor.close().

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.

The general syntax for using the getElementById method of document object is as follows:

document.getElementById(id)

for 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="520"
     height="400"></p>       

   </body>
</
html>

The output of the above program is:

Click for Dancing Doll

with the doll.jpg displays

When the above link is clicked, the doll.jpg displays with position shifted to the right. The statement document.getElementById('doll') gives reference to the object with id as doll and the attributes are applied to that object.

getElementsByName:

The getElementById 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.

The general syntax for using the getElementsByName method of a document object is as follows:

document.getElementsByName(name)

for 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" />

   </body>
</
html>

The output of the above program is

 

Ads

and a button with the message:

Click to see the number of input box

and when this is clicked, the output appears as:

2

The statement document.getElementsByName("Exforsys") returns the object with object name Exforsys. There are two input box as objects with name Exforsys, so the result displayed is 2.



 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments