You are on page 1of 9

www.w3schools.

com Document Object


Write text to the output with document.write() Write formatted text to the output with document.write() Return the number of anchors in a document Return the innerHTML of the first anchor in a document Return the number of forms in a document Return the name of the first form in a document Return the number of images in a document Return the id of the first image in a document Return the number of links in a document Return the id of the first link in a document Return all name/value pairs of cookies in a document Return the domain name of the server that loaded the document Return the date and time the document was last modified Return the URL of the document that loaded the current document Return the title of a document Return the full URL of a document Open an output stream, and add some text Open an output stream in a new window, and add some text Difference between write() and writeln() Alert innerHTML of an element with a specific ID Alert the number of elements with a specific name Alert the number of elements with a specific tagname <html> <body> <script type="text/javascript"> document.write("Hello World!"); </script> </body> </html>

Output

Hello World!
2 <html> <body>

<script type="text/javascript"> document.write("<h1>Hello World!</h1>"); </script>

</body> </html> Output Hello World! 3 <html> <body>

<a name="html">HTML Tutorial</a><br /> <a name="css">CSS Tutorial</a><br /> <a name="xml">XML Tutorial</a><br /> <a href="/js/">JavaScript Tutorial</a>

<p>Number of anchors: <script type="text/javascript"> document.write(document.anchors.length); </script></p>

</body> </html>

Output

HTML Tutorial CSS Tutorial XML Tutorial JavaScript Tutorial Number of anchors: 3
4 <html> <body>

<a name="html">HTML Tutorial</a><br /> <a name="css">CSS Tutorial</a><br /> <a name="xml">XML Tutorial</a>

<p>innerHTML of the first anchor: <script type="text/javascript"> document.write(document.anchors[0].innerHTML); </script> </p>

</body> </html> Output

HTML Tutorial CSS Tutorial XML Tutorial innerHTML of the first anchor: HTML Tutorial

5 <html> <body>

<form name="Form1"></form> <form name="Form2"></form> <form></form>

<p>Number of forms: <script type="text/javascript"> document.write(document.forms.length); </script></p>

</body> </html> Output

Number of forms: 3
6 <html> <body>

<form name="Form1"></form> <form name="Form2"></form> <form></form>

<p>Name of first form: <script type="text/javascript"> document.write(document.forms[0].name); </script></p> </body> </html> Output

Name of first form: Form1


7 <html> <body>

<img border="0" src="klematis.jpg" width="150" height="113" /> <img border="0" src="klematis2.jpg" width="152" height="128" />

<p>Number of images: <script type="text/javascript"> document.write(document.images.length); </script></p> </body> </html> Output

Number of images: 2
8 <html> <body>

<img id="klematis lilac" border="0" src="klematis.jpg" width="150" height="113" /> <img id="klematis pink" border="0" src="klematis2.jpg" width="152" height="128" />

<p>Id of first image: <script type="text/javascript"> document.write(document.images[0].id); </script></p>

</body> </html> Output

Id of first image: klematis lilac


9 <html> <body>

<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap" /> <map name="planetmap"> <area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun" /> <area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury" /> <area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus" /> </map>

<p><a href="/js/">JavaScript Tutorial</a></p>

<p>Number of areas/links: <script type="text/javascript"> document.write(document.links.length); </script></p>

</body> </html> Output

JavaScript Tutorial Number of areas/links: 4


10 <html> <body>

<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap" /> <map name="planetmap"> <area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun" /> <area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury" /> <area id="venus" shape="circle" coords="124,58,8" href="venus.htm" alt="Venus" /> </map>

<p><a id="javascript" href="/js/">JavaScript Tutorial</a></p>

<p>Id of first area/link: <script type="text/javascript"> document.write(document.links[0].id); </script></p>

</body> </html> Output

JavaScript Tutorial Id of first area/link: sun


11

You might also like