Javas Script 7th Class
Javas Script 7th Class
DOM
DOM compatibility
If you want to write a script with the flexibility to use either W3C DOM
or IE 4 DOM depending on their availability, then you can use a
capability-testing approach that first checks for the existence of a
method or property to determine whether the browser has the
capability you desire. For example −
if (document.getElementById) {
// If the W3C method exists, use it
} else if (document.all) {
// If the all[] array exists, use it
} else {
// Otherwise use the legacy DOM
}
<html>
<head>
<title>JavaScript DOM</title>
</head>
<body>
<p>Hello DOM!</p>
</body>
</html>
Code language: HTML, XML (xml)
Node Types
Each node in the DOM tree is identified by a node type. JavaScript uses
integer numbers to determine the node types.
Node.PROCESSING_INSTRUCTION_NODE 7 A ProcessingInstruction of
an XML document, such as <?xml-
stylesheet … ?>.
node.nodeType
Code language: CSS (css)
You can compare the nodeType property with the above constants to
determine the node type. For example:
if (node.nodeType == Node.ELEMENT_NODE) {
// node is the element node
}
Code language: JavaScript (javascript)
The values of these properites depends on the node type. For example,
if the node type is the element node, the nodeName is always the same as
element’s tag name and nodeValue is always null.
For this reason, it’s better to test node type before using these
properties:
if (node.nodeType == Node.ELEMENT_NODE) {
let name = node.nodeName; // tag name like <p>
}
Code language: JavaScript (javascript)
Sometime, it’s easy to confuse between the Node and the Element.
A node is a generic name of any object in the DOM tree. It can be any
built-in DOM element such as the document. Or it can be any HTML
tag specified in the HTML document like <div> or <p>.
In other words, the node is generic type of the element. The element is
a specific type of the node with the node type Node.ELEMENT_NODE.
Node Relationships
Any node has relationships to other nodes in the DOM tree. The
relationships are the same as the one described in a traditional family
tree.
For example, <body> is a child node of the <html> node, and <html> is
the parent of the <body> node.
The <body> node is the sibling of the <head> node because they share the
same immediate parent, which is the <html> element.
<html>
<head>
<title>Form Validation</title>
<script type = "text/javascript">
<!--
// Form validation code will come here.
//-->
</script>
</head>
<body>
<form action = “data base name" name = "myForm" onsubmit =
"return(validate());">
<table cellspacing = "2" cellpadding = "2" border =
"1">
<tr>
<td align = "right">Name</td>
<td><input type = "text" name = "Name" /></td>
</tr>
<tr>
<td align = "right">EMail</td>
<td><input type = "text" name = "EMail" /></td>
</tr>
<tr>
<td align = "right">Zip Code</td>
<td><input type = "text" name = "Zip" /></td>
</tr>
<tr>
<td align = "right">Country</td>
<td>
<select name = "Country">
<option value = "-1" selected>[choose
yours]</option>
<option value = "1">USA</option>
<option value = "2">UK</option>
<option value = "3">INDIA</option>
</select>
</td>
</tr>
<tr>
<td align = "right"></td>
<td><input type = "submit" value = "Submit"
/></td>
</tr>
</table>
</form>
</body>
</html>
Output
JavaScript - Multimedia
The JavaScript navigator object includes a child object
called plugins. This object is an array, with one entry for each plug-in
installed on the browser. The navigator.plugins object is supported
only by Netscape, Firefox, and Mozilla only.
Example
Here is an example that shows how to list down all the plug-on
installed in your browser −
Live Demo
<html>
<head>
<title>List of Plug-Ins</title>
</head>
<body>
<table border = "1">
<tr>
<th>Plug-in Name</th>
<th>Filename</th>
<th>Description</th>
</tr>
Output
<body>
<script language = "JavaScript" type = "text/javascript">
media = navigator.mimeTypes["video/quicktime"];
if (media) {
document.write("<embed src = 'quick.mov' height =
100 width = 100>");
} else {
document.write("<img src = 'quick.gif' height = 100
width = 100>");
}
</script>
</body>
</html>
Output
NOTE − Here we are using HTML <embed> tag to embed a
multimedia file.
Controlling Multimedia
Let us take one real example which works in almost all the browsers
−
Live Demo
<html>
<head>
<title>Using Embeded Object</title>
<body>
<embed id = "demo" name = "demo"
src = "http://www.amrood.com/games/kumite.swf"
width = "318" height = "300" play = "false" loop =
"false"
pluginspage =
"http://www.macromedia.com/go/getflashplayer"
swliveconnect = "true">