20_Programming for the Web
20_Programming for the Web
Chapter 20
Learning Objectives
Word/ Meaning
phrase
alert Displays a box containing a message to the user / containing results
array Arrays are indicated by square brackets and are used to store multiple values in a
single variable, each of which can be accessed by referring to an index number. Array
items are separated by commas.
bookmark An identifier put into the body of the html code to specify where script results are
placed in the webpage
Boolean Booleans can only have either of two values: true or false.
conditional Allows a program to perform different actions depending on the result of a condition;
statement the most commonly used are: if, if…else, if…else if, switch
confirm Displays a box that the user needs to click to continue / cancel
function A block of code designed to perform a particular task that can be called anywhere in
a script, and any number of times
Key terms
Word/ Meaning
phrase
global Variable that can be used in functions other than the function in which it is declared
variable or other parts of the script
local variable Variable available only within the function in which it is declared
loop Repeats a task until a certain condition is met; the most commonly used are: while,
statement do…while, for, for…in
number Numbers are one of JavaScript data types. They can be written with or without
decimals, and can use scientific notation for extra large or extra small numbers.
object Objects are indicated by curly brackets and are containers for properties called
named values. For example an object such as a blue bicycle would have a named
value of colour:”blue”. An objects properties use the following convention -
name:value, name:value, name:value, etc.
operator Symbol that indicates a specific process: arithmetic, string,
assignment, comparison, logical, or conditional
Key terms
Word/ Meaning
phrase
pop-up Alert, Prompt, Confirm boxes
prompt Dialog box that asks the user for input and assigns the input to a variable
script A high-level programming language (set of instructions), such as JavaScript that is
‘interpreted’ rather than compiled (e.g. when a web page is displayed)
string A string is text like a letter, word or phrase. Strings are indicated by quote marks and
either single or double quote marks can be used. Quote marks can be used within a
string, but they mustn’t match the quote marks that surround the string.
variable Named container to which data is assigned; can contain the following data types: text
(strings), number, boolean, array, object
Introduction
1. document.write() will output the text (content) inside the bracket to the web
page.
[Pg. 470, Worked Example 20.01]
2. .innerHTML is used when you need to name elements within your HTML code
and use script tag to instruct JavaScript to add the text to the document in the
element with the specific ID.
[Pg. 471, Worked Example 20.02]
4. console.log is used to make your web page record events, write messages to
etc. that do not actually appear on the web page, but appear in a log, which is
JavaScript (Part 1)
5. Changing images
[Page 475, Practical Activity 20.4]
2. Identifier naming
namingrules
rules are:
are:
can contain alphabets, numbers or underscore (_) but do not start with number or
but
underscore.
must not contain space; can join together multiple words, using lower case for the
whole first word and then capitalize subsequent words.
make variable names intuitive/meaningful, so they describe the data they contain.
variables are case sensitive, for example, myage is a different variable
from myAge.
avoid using JavaScript reserved words, for example var, function, let, and for.
clovis
Clovis
aileenNatasha
aileennatasha
clovis_wong
clovis_Wong
thisIsAGoodDay
JavaScript (Part 2) – Variables, Operators and
Functions
3. A variable declaration refers to the statement that tells the program that you need
a space in memory and what its identifier will be. For example, var name;
4. An assignment operator (=) adds a value to a variable.
For example:
name = “Lee”;
age = 17;
isSold = True;
5. The data stored in a variable will be of a set of data type. For example, String,
Number, Boolean, Array, Object.
* var name;
var name = ‘Lee’;
Data types are not declared when declaring a variable, it is assumed when a
value is given/assign to the variable.
JavaScript (Part 2) – Variables, Operators and
Functions
6. Type conversion:
a) to String
var stringNumber = String(999);
b) to integer
var numberString = Number(“999”);
c) Boolean to number
var numberBoolean = Number(true);
8. Function is:
a set of instructions that perform a specific task.
independent code that only runs if it is called.
very useful for removing repeated code, especially when it may need to be
repeated at different positions/locations in a program.
• Functions can be:
called from within the JavaScript code
called when an event occurs such as clicking a button
automatically run (self-invoked).
• A function can take values from the program that calls it; these are called
parameters. It can also return a value back to the program that called it.
• When a function is being created, variable(s) can be created locally (inside the
function) or globally (outside of the function).
A local variable exists only when the function is running; it can only be
accessed (e.g. output or changed) from within the function.
A global variable exists as soon as the web page loads, and permanently
exists;
it can be accessed anywhere within the JavaScript.
JavaScript (Part 3) – Form elements
9. All the code to display buttons, drop-down boxes etc. comes within the
<form></form> tag.
a) button
When user clicks on a HTML button, it can be programmed to call a JavaScript
function.
9. All the code to display buttons, drop-down boxes etc. comes within the
<form></form> tag.
b) text box
You can enter text into a text box, and then access the data and use it.
An action is needed to tell JavaScript to get the data from the text box.
9. All the code to display buttons, drop-down boxes etc. comes within the
<form></form> tag.
c) drop-down box
It gives the user options to choose from.
d) radio button
It gives the user a range of options that they can see, but can only select
one.
10.Detecting events
a) onload - When attach to an element, it will run when the element is called or
loaded.
b) onchange – Can be attached to an element that the user can change, e.g. drop-
down box.
c) onmouseover – will run when user moves the cursor over an element, e.g. an
image.
d) onmouseout – will run when the user moves the cursor away from an element,
e.g. an image.
e) onkeydown – will run when user interact with a text box, e.g. clicks a key on the
keyboard.
JavaScript (Part 5) – Show and hide HTML
elements
HTML elements (e.g. paragraphs) can be made visible (appear on screen), and invisible
(cannot be seen)
document.getElementById(“text1”).style.visibility = “hidden”;
document.getElementById(“text1”).style.visibility = “visible”;
OR
document.getElementById(“text1”).style.display = “none”;
document.getElementById(“text1”).style.display = “inline”;
• A conditional statement allows a program to perform different actions depending on the result of a
condition: a different block of code is run for each decision branch.
2. if ... else – allows the program to run an alternative block of code if the condition is not met.
• A conditional statement allows a program to perform different actions depending on the result of a
condition: a different block of code is run for each decision branch.
4. switch – is used to select one of many blocks of code to be executed, depending on the value of a
variable; if nothing matches, a default condition will be used.
switch (resulting value of a variable) {
case condition 1:
statement(s);
break;
case condition 2:
statement(s);
break;
default:
statement(s);
}
JavaScript (Part 6) – Conditional Statements
Activity/Homework:
[20.12 Selection: Worked Example 20.14, Worked Example 20.15, Questions (Page
499)]
JavaScript (Part 6) – Conditional Statements
• Ternary operator – a short-hand if... else which can be used to replace multiple lines
of code with a single line.
instead of:
if (condition is true) {
expressionTrue;
} else {
expressionFalse;
}
• It is a data structure that allows you to store multiple values (of the same data type)
under one name.
• A loop is used when you want to perform the same action multiple times.
• There are two types of loop: count controlled (for) and condition controlled
(while and do/while)
• for: you need to know the number of times the loop is to run.
• for/in: it is used if you have a JavaScript object that you need to repeat through
each element to output them all.
var film = {title:”The house”, genre:”Drama”, length:96, releaseYear:2013};
var count;
var check=true;
while (check=true){
document.write(“It’s true”)
} //this while
loop does not use a counter.
JavaScript (Part 8) –
Loops/Repetitions/Iterations
• do/while: checks at the end of the loop; it will run at least once.
var count = 0;
do{
document.write(count*count)
count++;
}
while(count<=12);
setTimeout(function(){
prompt(“Do you want to continue?”);
}, 2000);
setInterval(function(){
alert(“Hello”);
}, 1000);
[Worked Example 20.18, Worked Example 20.19, Practical Activity 20.20 – Q2,
Q4]
[Practical Skills Task 7, Practical Skills Task 6]
JavaScript (Part 10) – String manipulation
• Allows you to perform actions on a string and extract information from it.
• When counting letters in a string, the first letter is letter 0, and the second is letter
1,
the third is letter 2, and so on.
1. every
2. some
3. filter
4. forEach
5. map
• Errors can occur in a variety of places in a program and for a variety reasons.
• Write the JavaScript in a separate document and then import into the HTML
document.
create a separate document and write the JavaScript code inside.
save the file with a name and the extension .js
call the code within the HTML document.
<html>
<script src=“javascriptparagraph.js”></script>
<html>