Module 5-Advanced Scripting
Module 5-Advanced Scripting
APPLICATIONS
MODULE 5 –
Advanced Scripting
• JavaScript and Objects (SDL)
• Javascript own objects (SDL)
• The Document Object Model (DOM)
• Web browser Environments
• Forms and validation
Section 5 of 8
JAVASCRIPT OBJECTS
JavaScript Objects
Objects not Classes
❑Arrays are one of the most used data structures. In practice, this
class is defined to behave more like a linked list in that it can be
resized dynamically, but the implementation is browser specific,
meaning the efficiency of insert and delete operations is
unknown.
❑The following code creates a new, empty array named greetings:
❖The String class has already been used without us even knowing
it.
❖Constructor usage
❑The Date class is yet another helpful included object you should
be aware of.
❑It allows you to quickly calculate the current date or create date
objects for particular dates.
❑To display today’s date as a string, we would simply create a
new object and use the toString() method.
var d = new Date();
// This outputs Today is Mon Nov 12 2012 15:40:19 GMT-0700
alert ("Today is "+ d.toString());
Window
Property Description
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
Document Object
One root to ground them all
Method Description
Method Description
<p id="demo"></p>
<script>
const x = document.getElementsByClassName("intro");
document.getElementById("demo").innerHTML =
'The first paragraph (index 0) with class="intro" is: ' + x[0].innerHTML;
</script>
</body>
</html>
Changing HTML Elements
Property Description
element.innerHTML = new html content Change the inner HTML of an element
Method Description
element.setAttribute(attribute, value) Change the attribute value of an HTML
element
Changing HTML Content
<!DOCTYPE html>
<html>
<body>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
Adding and Deleting Elements
Method Description
document.createElement(element) Create an HTML element
<div id="container">
<p id="para1">This is the first paragraph.</p>
<p id="para2">This is the second paragraph.</p>
</div>
<script>
// 1. Create a new element
const newParagraph = document.createElement("p");
newParagraph.textContent = "This is a new paragraph created using createElement.";
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript DOM Manipulation</title>
</head>
<body>
<div id="container">
<p id="para1">This is the first paragraph.</p>
<p id="para2">This is the second paragraph.</p>
</div>
<script>
// 2. Append the new element as the last child of the container
const container = document.getElementById("container");
container.appendChild(newParagraph);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript DOM Manipulation</title>
</head>
<body>
<div id="container">
<p id="para1">This is the first paragraph.</p>
<p id="para2">This is the second paragraph.</p>
</div>
<script>
// 3. Remove an existing child element
const para1 = document.getElementById("para1");
container.removeChild(para1);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript DOM Manipulation</title>
</head>
<body>
<h2>JavaScript DOM Manipulation Example</h2>
<div id="container">
<p id="para1">This is the first paragraph.</p>
<p id="para2">This is the second paragraph.</p>
</div>
<script>
// 4. Replace an existing child with a new child
const anotherParagraph = document.createElement("p");
anotherParagraph.textContent = "This paragraph will replace the second one.";
const para2 = document.getElementById("para2");
container.replaceChild(anotherParagraph, para2);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript DOM Manipulation</title>
</head>
<body>
<div id="container">
<p id="para1">This is the first paragraph.</p>
<p id="para2">This is the second paragraph.</p>
</div>
<script>
// 5. Use document.write to display additional text (not recommended for modern
use)
document.write("<p>This text is added using document.write.</p>");
</script>
</body>
</html>
Adding Events Handlers
Method Description
document.getElementById(id).onclick Adding event
= function(){code} handler code to
an onclick
event
Accessing nodes
getElementById(), getElementsByTagName()
Element node Object
Property Description
JAVASCRIPT EVENTS
JavaScript Events
<script>
document.getElementById("myBtn").addEventListener("click", function() {
alert("Hello World!");
});
</script>
</body>
</html>
Listener Approach
Using functions
Event Description
onclick The mouse was clicked on an element
ondblclick The mouse was double clicked on an element
onmousedown The mouse was pressed down over an element
onmouseup The mouse was released over an element
onmouseover The mouse was moved (not clicked) over an
element
onmouseout The mouse was moved off of an element
onmousemove The mouse was moved while over an element
<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The onmousedown Event</h2>
<p>Clock the text below!</p>
<p id="myP" onmousedown="mouseDown()" onmouseup="mouseUp()">
The mouseDown() function sets the color of this text to red.
The mouseUp() function sets the color of this text to blue.
</p>
<script>
function mouseDown() {
document.getElementById("myP").style.color = "red";
}
function mouseUp() {
document.getElementById("myP").style.color = "blue";
}
</script>
</body>
</html>
Output
Keyboard events
Event Description
onkeydown The user is pressing a key (this
happens first)
onkeypress The user presses a key (this
happens after onkeydown)
onkeyup The user releases a key that was
down (this happens last)
Keyboard events
Example
❑Frame events are the events related to the browser frame that
contains your web page.
❑The most important event is the onload event, which tells us an
object is loaded and therefore ready to work with. If the code
attempts to set up a listener on this not-yet-loaded <div>, then
an error will be triggered.
window.onload= function(){
//all JavaScript initialization here.
}
Frame Events
Table of frame events
Event Description
onabort An object was stopped from loading
FORMS
Validating Forms
You mean pre-validating right?
❖If you want to ensure a checkbox is ticked, use code like that
below.
var inputField=document.getElementByID("license");
if (inputField.type=="checkbox")
{
if (inputField.checked)
//Now we know the box is checked
}
Validating Forms
Number Validation
Submitting Forms