Computer 8 Module 14
Computer 8 Module 14
Application Development
Module 14 (Week 1 & 2)
What’s In
You learned in the previous examples the use of the alert() and prompt() function to
display popup boxes. The confirm() function can also be used to create a popup box that asks
users to respond to a question.
Syntax
confirm(message)
Example:
var a
var n
var c
a = comfirm(“The program will compute for the square of a number.
Continue?”)
If (a==true)
{
n = parseInt(prompt(“Enter a number”,0))
Alert(“The square of the number is “+”n*n)
}
A value of true will be returned if the OK button is clicked. A value of false will be returned if
the Cancel button is clicked or the Esc key is pressed. Use the confirm() function to give the user a
warning before a task is performed.
Q. OTHER OBJECTS
Aside from the objects you encountered earlier in this chapter, there are additional objects
that you can use to manipulate or retrieve information about the page. There is no need to
memorize the properties and methods of the objects. Just go over and find out their possible uses.
Some of them will be used in the exercise later.
The window object is the main or top-level object in Javascript. It has the following properties
and methods that you can use.
A. WNDOW OBJECT
B. DOCUMENT OBJECT
D. LOCATION OBJECT
The Location object is an object created by Javascript that contains information about
the page is currently loaded.
The HTML Document Object Model (HTML DOM) provides a standardized way to access
objects in HTML. All HTML elements have their own set of properties and methods that you can use.
The History and Location objects are created by Javascript and are not part of the HTML DOM.
Create an HTML file and copy the script below to see how the
contents of a multi-line textbox can be accessed and changed using
Javascript.
<html>
<head>
<script type="text/javascript">
function addtext(t)
{
var d
d= document.getElementByld("displaybox")
d.value = d.value +t
}
</script>
</head>
<body>
<p align="center"
onclick="addtext('click')"
ondblclick="addtext('double-click')"
onmouseover="addtext('mouseover')"
onmouseout="addtext('mouseout')">
click me
</p>
<textarea cols="30" rows="10" 1d="displaybox"></textarea>
</body>
</html>
The textarea (multi-line textbox) has an Id value of displaybox while the paragraph element
uses events to trigger the addtext() function. The textarea in the sample script is created by using the
<textarea> tag:
You will learn more about the textarea and other similar objects in the next chapter. In the
meantime, focus on how to access and modify the content of the textarea using Javascript.
The addtext() function modifies values of the textbox by performing the following:
1. It can also be used to create a popup box that asks users to respond to a question.
2. A function that accept a value through variable t
3. A variable is declared
4. It creates a reterence to the textarea object using the document. getElementByld() function.
5. The value stored in the textarea will be replaced by its old value plus the new value which is stored in
variable t.
6. Its description is to provide a standardized way to access objects in HTML.
7. Its description is to return the title of the current page as specified in the <title> and </title> tags.
8. Its description is to print the contents of the current window.
9. Its description is to insert text or HTML elements into the page.
10. Its description is to return the URL of the current page.
Direction: Explain what the code does. (Practice the code and do not submit your answer).
<html>
<head>
<title>DOM</title>
<script type = "text/javascript">
function changeBackground(eventDone){
if (eventDone == 1){
document.body.style.backgroundColor = "red"
document.getElementById("context").innerHTML = "CLICKED"
}
else if(eventDone == 2)
{
document.body.style.backgroundColor = "blue"
document.getElementById("context").innerHTML = "DOUBLE-CLICKED"
}
else if(eventDone == 3)
{
document.body.style.backgroundColor = "green"
document.getElementById("context").innerHTML = "MOUSEOVER"
}
Else
{
document.body.style.backgroundColor = "brown"
document.getElementById("context").innerHTML = "MOUSEOUT"
}
}
</script>
</head>
<body>
ANSWER SHEET
I. What’s More.
Direction: Identify the following questions (refer to page 7). [10 points]
1. 6.
2. 7.
3. 8.
4. 9.
5. 10.
1. 6.
2. 7.
3. 8.
4. 9.
5. 10.
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
Direction: Explain what the code does. (Practice the code and do not submit your answer).
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________