Topic 9: Understanding The Functions of Javascript
Topic 9: Understanding The Functions of Javascript
Every web page resides inside a browser window which can be considered as an object.
A Document object represents the HTML document that is displayed in that window. The Document
object has various properties that refer to other objects which allow access to and modification of
document content.
The way document content is accessed and modified is called the Document Object Model, or DOM.
The Objects are organized in a hierarchy. This hierarchical structure applies to the organization of
objects in a Web document.
When a web page is loaded, the browser creates a Document Object Model of the page.
The HTML DOM model is constructed as a tree of objects:
Window object: Top of the hierarchy. It is the outmost element of the object hierarchy.
Document object: Each HTML document that gets loaded into a window becomes a document
object. The document contains the contents of the page.
Form object: Everything enclosed in the <form>….</form> tags set the form object.
1
Form control elements: The form object contains all the elements defined for that object such
as text fields, buttons, radio buttons, and checkboxes.
JavaScript can change all the HTML elements, HTML attributes, CSS styles …. in
the page.
Example 1:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Example 2:
<html>
<head>
<!--
function myFunc()
2
alert("Document Title : " + ret ); You are calling to document (DOM)
//-->
</script>
</head>
<body>
</form>
<form name="SecondForm">
</form>
</body>
</html>
More Examples
<!DOCTYPE html>
<html>
<body>
3
Alert boxes/ Message boxes
<h1>My First Web Page</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
<p id="demo"></p>
</body>
</html>
4
Regular Expressions
When you search for data in a text, you can use this search pattern to describe what
you are searching for.
<html>
<body>
<p>Search a string for "NICE", and display the position of the match:</p>
<p id="demo"></p>
<script>
var n = str.search(/nice/i);
document.getElementById("demo").innerHTML = n;
</script>
</body>
</html>
Comments
<html>
<body>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
multi
line
comment
*/
</script>
</body>
</html>
If-Else Conditions
<html>
<body>
<p id="demo"></p>
<script>
function myFunction() {
var greeting;
} else {
6
greeting = "Good evening";
document.getElementById("demo").innerHTML = greeting;
</script>
</body>
</html>
<html>
<body>
<p id="demo"></p>
<script>
var day;
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
7
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
</script>
</body>
</html>
While Loop
<html>
<body>
<p id="demo"></p>
<script>
var i = 0;
8
while (i < 20) {
i++;
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>