Internet Based Programming: Frontend Web Development
Internet Based Programming: Frontend Web Development
05/18/2025 2
1
05/18/2025
Text:
Internet
Based
Programming
05/18/2025 3
• With the object model, JavaScript gets all the power it needs to create dynamic
HTML:
JavaScript can change all the HTML elements in the page.
JavaScript can change all the HTML attributes in the page.
JavaScript can change all the CSS styles in the page.
JavaScript can remove existing HTML elements and attributes.
JavaScript can add new HTML elements and attributes.
JavaScript can react to all existing HTML events in the page.
JavaScript can create new HTML events in the page.
05/18/2025 4
2
05/18/2025
05/18/2025 5
05/18/2025 6
3
05/18/2025
05/18/2025 7
05/18/2025 8
4
05/18/2025
05/18/2025 9
<!DOCTYPE html>
<html>
<body>
This example <div id="main">
finds the element <p>Finding HTML Elements by Tag Name</p>
with id="main", <p>This example demonstrates the <b>getElementsByTagName</b> method.</p>
</div>
and then finds <p id="demo"></p>
all <p> elements <script>
const x = document.getElementById("main");
inside "main": const y = x.getElementsByTagName("p");
document.getElementById("demo").innerHTML =
`The first paragraph (index 0) inside "main" is: <b>${y[0].innerHTML}</b>`;
</script>
</body>
</html>
05/18/2025 10
5
05/18/2025
<!DOCTYPE html>
This example returns <html>
<body>
a list of all elements <p class="intro">Finding HTML Elements by Class Name.</p>
with class="intro“: <p class="intro">Hello World!</p>
<p id="demo"></p>
<script>
const x = document.getElementsByClassName("intro");
document.getElementById("demo").innerHTML =
`The first paragraph (index 0) with class="intro" is: <b>${x[0].innerHTML}</b>`;
</script>
</body>
</html>
05/18/2025 11
05/18/2025 12
6
05/18/2025
05/18/2025 13
05/18/2025 14
7
05/18/2025
<!DOCTYPE html>
<html>
<body>
<h1 id="hd1">Old Heading</h1>
<script>
const element = document.getElementById("hd1");
element.innerHTML = "New Heading";
</script>
<p>JavaScript changed "Old Heading" to "New Heading".</p>
</body>
</html>
05/18/2025 15
<!DOCTYPE html>
<html>
<body>
<img id="myImage1" src="Pics/googlelogo.png">
<img id="myImage2" src="Pics/googlelogo.png">
<script>
document.getElementById("myImage2").src = "Pics/tree.png";
</script>
</body>
</html>
05/18/2025 16
8
05/18/2025
05/18/2025 17
05/18/2025 18
9
05/18/2025
05/18/2025 19
10
05/18/2025
<!DOCTYPE html>
<html>
<body>
<h2>The onclick Attribute</h2>
<h2 onclick="this.innerHTML='Hello!'">Click on me!</h2>
</body>
</html>
05/18/2025 22
11
05/18/2025
05/18/2025 23
05/18/2025 24
12
05/18/2025
<!DOCTYPE html>
<html>
<body onload="msg()">
<h1>JavaScript HTML Events</h1>
<h2>The onload Attribute</h2>
<script>
function msg() {
alert("Page is loading");
}
</script>
</body>
</html>
05/18/2025 25
05/18/2025 26
13
05/18/2025
05/18/2025 27
05/18/2025 28
14
05/18/2025
05/18/2025 29
05/18/2025 30
15
05/18/2025
05/18/2025 31
• In HTML event attributes, we controlled JavaScript from HTML since these attributes are trigging
on HTML elements and causing JavaScript codes to be execute. In EventListener we are doing the
opposite, we are controlling HTML elements by JavaScript addEventListener() method.
• The addEventListener() method attaches an event handler to the specified element.
• Syntax:
05/18/2025 32
16
05/18/2025
<body>
<button id="myBtn">Try it</button>
<p id="demo"></p>
<script>
document.getElementById("myBtn").addEventListener("click", function () {
document.getElementById("demo").innerHTML = "Computer Science";
});
</script>
</body>
05/18/2025 33
05/18/2025 34
17
05/18/2025
<!DOCTYPE html>
<html>
<body>
<p>Try resizing this browser window to trigger the "resize" event handler.</p>
<p id="demo"></p>
<script>
window.addEventListener("resize", function () {
document.getElementById("demo").innerHTML = `The window size is:
${window.innerWidth} * ${window.innerHeight}`;
});
</script>
</body>
</html>
05/18/2025 35
• Passing parameters: When passing parameter values, use an "anonymous function" that
calls the specified function with the parameter:
<body>
<p>Click the button to perform a calculation.</p>
<button id="myBtn">Try it</button>
<p id="demo"></p>
<script>
let p1 = 5;
let p2 = 7;
document.getElementById("myBtn").addEventListener("click", function () {
myFunction(p1, p2);
});
function myFunction(a, b) {
document.getElementById("demo").innerHTML = a * b;}
</script>
</body>
05/18/2025 36
18
05/18/2025
05/18/2025 37
function myFunction() {
document.getElementById("demo").innerHTML = Math.random();}
function removeHandler() {
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);}
</script>
</body>
05/18/2025 38
19
05/18/2025
05/18/2025 39
05/18/2025 40
20
05/18/2025
05/18/2025 41
05/18/2025 42
21
05/18/2025
05/18/2025 43
05/18/2025 44
22
05/18/2025
05/18/2025 45
05/18/2025 46
23
05/18/2025
05/18/2025 47
05/18/2025 48
24
05/18/2025
05/18/2025 49
05/18/2025 50
25