Skip to content
Cambridge AS & A Level9626

Programming for the web

Information Technology 9626 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Programming for the web notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Programming for the web notes as text: skim, search, and jump between subtopics.

~15 min read

1. HTML events

An HTML event is a change in a web page, caused by a user or by the browser. JavaScript can start when an event happens.

`onmouseover` starts a script when the mouse pointer moves over an element. `onmouseout` starts it when the pointer moves away from the element. `onload` starts it when the page has finished loading into the browser.

`onclick` reacts to a mouse click, `onchange` to a change in an input field and `onkeydown` to a key being pressed.

Examiner insight

“A common error was to state that the event happens when the pointer is ‘not over’ the element of area.” “The important point is that the web page must be fully loaded into the browser to trigger the HTML event.”

Worked example 15 marks

Programmers can use JavaScript to make a web page react when an HTML event occurs.

(a) Describe what is meant by an HTML event.

(b) Describe the action that causes each of the following HTML events to start:(i) onmouseover()(ii) onmouseout()(iii) onload()

  1. 1

    (a) an action or change in the web page [1]

  2. 2

    caused by a user or by the browser [1]

  3. 3

    (b)(i) `onmouseover`: the mouse pointer moves over an element [1]

  4. 4

    (ii) `onmouseout`: the mouse pointer moves away from an element [1]

  5. 5

    (iii) `onload`: the page has completely finished loading into the browser [1]

2. Scripts and output

JavaScript is enclosed in `<script>` tags in the HTML page, or kept in an external script.

To change the page, JavaScript writes into an HTML element with `innerHTML`. Other outputs are `document.write()` into the HTML output, `window.alert()` for an alert box and `console.log()` for the browser console. Use `+` to join (concatenate) variables.

``` document.getElementById("QPname").innerHTML = b + " " + a; ```

Examiner insight

“A common incorrect response was to repeat the question by stating that it is used to ‘output data for display’ in a browser.” “Strong answers included references to it being used for testing or debugging purposes, writing messages into the browser console and the information being in brackets.”

Worked example 16 marks

Data that is output from JavaScript code is passed to HTML so that a web browser can display it. The web page code is below. The line numbers are only for reference.

``` 1 <html> 2 <body> 3 <h2>This code displays the question paper name in a web browser</h2> 4 <br> 5 <br> 6 <p id="QPname"></p> 7 8 <script> 9 var a = "9626/32"; 10 var b = "Paper 3 Advanced Theory"; 11 document.getElementById("QPname").innerHTML = b + " " + a; 12 </script> 13 14 </body> 15 </html> ```

Describe how the code processes and passes the name of the question paper to the web browser for display.

  1. 1

    the HTML element with id `QPname` (line 6) is read [1]

  2. 2

    the JavaScript is enclosed in `<script>` tags [1]

  3. 3

    the paper number is assigned to variable `a` (line 9) [1]

  4. 4

    the paper name is assigned to variable `b` (line 10) [1]

  5. 5

    line 11 uses `innerHTML` and `+` to join the variables, with no quotes around them; `" "` inserts a space [1]

  6. 6

    the order of the variables in line 11 sets the order of display, and the joined text is passed to the HTML for display [1]

3. Hiding and showing elements

A style is changed with `document.getElementById(id).style.property = new style`. `id` refers to the HTML element being changed, and the change is to its inline style.

`visibility` sets whether an element can be seen. `visible` shows it, `hidden` hides it but leaves a blank space, `collapse` hides a table row or cell, `inherit` copies the parent element and `initial` is the default value, replaced by the value wanted.

Setting `display` to `"none"` hides the whole element with no space left where it was.

Examiner insight

“Some candidates incorrectly described the ‘initial’ value as a variable to be assigned or printed to the screen.” “Some candidates gave invalid values that could be used in the statement instead of ‘initial’.”

Worked example 16 marks

The visibility of HTML elements in a webpage can be controlled by JavaScript statements that change the style property. The statement below sets the style property of an element to a default value.

``` document.getElementById(id).style.visibility = "initial" ```

Describe how this statement is used in JavaScript to alter the appearance of HTML elements.

  1. 1

    the statement is in a function, and `id` is passed to the function from a variable [1]

  2. 2

    `id` refers to the HTML element being affected, and it is unique [1]

  3. 3

    the change is to the inline style, setting whether the element can be seen [1]

  4. 4

    `initial` is replaced by the value wanted [1]

  5. 5

    `visible` shows the element; `hidden` makes it not visible and leaves a blank space where it was [1]

  6. 6

    `inherit` makes it the same as its parent; `collapse` in a table hides the row or cell and leaves a blank space [1]

4. Dialog boxes: alert, prompt and confirm

`alert()` shows a message in a box. `prompt()` shows an input box: the user types a value and clicks OK or Cancel. The value is returned to a variable, or NULL if the user cancels. The box blocks the script until the user responds.

`confirm()` shows a box with two options, OK and Cancel. It returns a Boolean: TRUE for OK and FALSE for Cancel.

The browser decides the position and appearance of a dialog box, not the programmer.

Examiner insight

“A number of candidates muddled their answers with references to other forms of dialog box in which the user types responses.” “Weaker answers gave vague references to ‘can’t check the entry’ or ‘have limited number of characters’ without any explanations.”

Worked example 16 marks

The code displays a person's full name (given name and family name) when it is entered into a web page. The line numbers are for reference only.

``` 1 <html> 2 <body> 3 <h2>This code collects a person's name and displays it</h2> 4 <br> 5 <br> 6 <p id="id1"></p> 7 <script> 8 var a = "Your name is " 9 const GName = prompt("Your given name please..."); 10 const FName = prompt("Your family name please..."); 11 document.getElementById("id1").innerHTML = a + GName + " " + FName; 12 </script> 13 </body> 14 </html> ```

Describe how the code collects and processes the given and family names so that the full name can be displayed on the web page.

  1. 1

    the JavaScript is enclosed in `<script>` tags; `a` holds the intro text [1]

  2. 2

    `prompt()` creates an input box twice: one for the given name and one for the family name [1]

  3. 3

    the user must enter a name before the page carries on, as `prompt()` blocks JavaScript [1]

  4. 4

    the names are assigned to variables `GName` and `FName` [1]

  5. 5

    line 11 uses `+` to concatenate `a`, `GName`, `" "` and `FName`, with no quotes around the variables [1]

  6. 6

    the combined name is passed to the element with id `id1` using `innerHTML` [1]

5. Functions and timing events

A function is code that runs when an event occurs, when invoked from code, or automatically (self-invoked).

`setTimeout()` runs code once, after a delay. `setInterval()` runs code repeatedly at set intervals. Both need two parameters and measure time in milliseconds.

Examiner insight

“This question was not well answered with a common mistake by candidates being to repeat the question e.g. both delay the start of the code.” “Differences in the effect on the code were not clearly described by most candidates but should have referred to e.g. setTimeout() delaying the execution of code which runs only once while setInterval() is used for repeated execution of the code at (pre-set) intervals.”

Worked example 16 marks

JavaScript code can be executed when an HTML event happens in a web browser. An example of an HTML event is when a user clicks a mouse button.

(a) Describe how the JavaScript in this HTML code changes the display of text on the web page when a mouse button is clicked.

``` 5 <h2 onclick="changeText(this)">Click on this line to see the name of this 9626 QP</h2> 6 7 <script> 8 function changeText(id) { 9 id.innerHTML = "A Level IT Paper 3"; 10 } 11 </script> ```

(b) Describe two other HTML events that can be used to trigger the execution of JavaScript code.

  1. 1

    (a) `onclick` captures the mouse click on the text in line 5 [1]

  2. 2

    control passes to the function in the script (line 8) [1]

  3. 3

    the parameter `id` is passed to the function [1]

  4. 4

    `id.innerHTML` changes the text in the `<h2>`, so the new text appears on the web page [1]

  5. 5

    (b) e.g. a web page has loaded (`onload`) [1]

  6. 6

    e.g. the mouse moves over an HTML element (`onmouseover`) [1]

6. Values, operators and comparison

A string is a collection of characters inside quotes. The other data types are number, Boolean, array and object.

Arithmetic operators include `+ - * / %`, `++` (adds 1) and `--` (takes 1 away). `%` gives the modulus, the remainder. Logical operators are AND, OR and NOT.

Comparison operators return TRUE or FALSE. `===` is TRUE if both the value and the type are equal. `!==` is TRUE if the value is not equal or the type is not equal.

Examiner insight

“Weak answers confused logical operators with arithmetic or other operators.”

Worked example 16 marks

JavaScript statements can contain comparison operators such as > and <.

(a) Describe the purpose of comparison operators.

(b) Explain the differences in function between the === and !== comparison operators.

  1. 1

    (a) used in logical operations to determine equality or inequality between variables or values [1]

  2. 2

    they return TRUE or FALSE as the result of the comparison [1]

  3. 3

    (b) `===` returns TRUE if both the value and the type are equal [1]

  4. 4

    `===` returns FALSE if either is not the same [1]

  5. 5

    `!==` returns TRUE if the value is not equal or the type is not equal [1]

  6. 6

    `!==` returns FALSE if the value and the type are both the same [1]

7. Conditions and loops

`if`, `else` and `else if` choose which code to run from a condition. `switch` stores a variable and tests a number of cases against it to decide the action. The ternary operator is a short form of a condition that reduces the lines of code.

In a `for` loop the number of loops depends on the outcome of testing a condition. A for in loop will iterate through the items of an array or the properties of an object.

Examiner insight

“Common mistakes were to confuse the operation of the two types of loop.” “Weak answers referred to switching between variables or switching between cases but were either vague or inaccurate.”

Worked example 16 marks

The JavaScript code below is executed when it is loaded into a web browser. The line numbers are for reference only.

``` 4 <p id="ALP3"></p> 6 <script> 8 const qp = ["A Level", "IT", "9626", "Paper", 3]; 10 let paper = ""; 11 for (let x in qp) { 12 paper += qp[x] + "<BR>"; 13 } 15 document.getElementById("ALP3").innerHTML = paper; 17 </script> ```

Explain how the code produces a list in the web browser with A Level, IT, 9626, Paper and 3 on separate lines.

  1. 1

    the JavaScript is embedded in the HTML, inside `<script>` tags [1]

  2. 2

    the element with id `ALP3` is empty at the start; array `qp` holds the text for display [1]

  3. 3

    variable `paper` is declared and set to empty [1]

  4. 4

    `for in` loops through the array, and `x` counts the array items during the loop [1]

  5. 5

    each time round, `paper` holds the array item plus `<BR>`, which forces a line break [1]

  6. 6

    the contents of `paper` are passed to the element with id `ALP3` by `innerHTML`; the loop stops when no items are left [1]

8. Flowchart and diagram symbols

A flowchart has these elements: input/output, decision, terminator (Start/Stop), process box, subroutine, connector and flow line.

A system flowchart has: input/output, process, single document output, multiple document output, magnetic disk file, magnetic tape file and display.

A data flow diagram has: process, data store, data source or destination, and duplication data source or destination. Use the conventional shapes, with arrows for the flow of data between the elements.

Examiner insight

“Some candidates confused the symbols, drew the same ones for every element or did not identify the elements correctly and so did not score the marks.” “Some candidates did not show all the data flow between the elements.”

Worked example 14 marks

State the four elements used in a data flow diagram.

  1. 1

    process [1]

  2. 2

    data store [1]

  3. 3

    data source or destination [1]

  4. 4

    duplication data source or destination [1]

Recap

  • An HTML event is a change in a web page caused by a user or by the browser.
  • onmouseover: pointer moves over an element. onmouseout: pointer moves away from it. onload: page fully loaded.
  • innerHTML writes into an element; document.write(), window.alert() and console.log() are other outputs.
  • display set to none leaves no space; visibility set to hidden leaves a blank space.
  • prompt() returns the value (or NULL); confirm() returns TRUE or FALSE.
  • setTimeout() runs code once after a delay; setInterval() repeats it.
  • === compares value and type; !== is TRUE if either differs.
  • Flowchart, system flowchart and data flow diagram each have their own conventional symbols.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. State when an `onload` event starts.1 mark
    Answer
    1. When the page has completely finished loading into the browser.

    Trap: Starting to load is not enough: the page must be fully loaded.

  2. State the name of the method that writes a message into the browser console, and one reason a programmer uses it.2 marks
    Answer
    1. `console.log()` [1], for testing or debugging [1].

    Trap: ‘To output data for display’ only repeats the question.

  3. A page contains `const n = prompt("Name?");`. The user clicks Cancel. State what is stored in `n`.1 mark
    Answer
    1. NULL is returned to the variable `n`.

    Trap: FALSE is what `confirm()` returns, not `prompt()`.

  4. State the type of value returned by `confirm()` and the value returned when the user clicks Cancel.2 marks
    Answer
    1. Boolean [1]; FALSE [1].

    Trap: Describing what the program does next: only the returned value is asked for.

  5. The variable `x` is 17. State (a) the value of `x % 5`, (b) the value of `x` after `x++`, (c) what the `%` operator is called.3 marks
    Answer
    1. (a) 17 ÷ 5 = 3 remainder 2, so 2 [1]; (b) 18 [1]; (c) modulus [1].

    Trap: `%` gives the remainder, not a percentage.

  6. State whether `5 === "5"` and `5 !== "5"` return TRUE or FALSE. Give a reason.3 marks
    Answer
    1. `5 === "5"` is FALSE and `5 !== "5"` is TRUE [2]: a number and a string, so the types are not equal [1].

    Trap: The values look the same, but `===` also compares the type.

  7. A paragraph has the id `note`. Write one JavaScript statement that hides it and leaves no space. State what `visibility = "hidden"` does differently.3 marks
    Answer
    1. `document.getElementById("note").style.display = "none"` [1]. `visibility = "hidden"` hides it [1] but leaves a blank space [1].

    Trap: The two properties are easy to swap: only `display` removes the space.

  8. State the output of this loop. ``` for (let i = 1; i < 10; i = i + 3) { console.log(i); } ```3 marks
    Answer
    1. `i` is 1, 4 and 7, and each is logged [1]. Next `i` is 10, and 10 < 10 is false, so the loop ends [1].
    2. Output: 1, 4, 7 [1].

    Trap: Expecting 10: examiners reported many candidates missed that the loop never reaches it.

  9. A quiz page runs this code when it loads. ``` setTimeout(check, 30000); function check() { const ans = confirm("Time is up. Submit now?"); window.alert(ans ? "Submitted" : "Keep going"); } ``` (a) State when `check` runs and how many times. (b) The user clicks Cancel. State the value stored in `ans`, its data type and the message the user sees. (c) The quiz should ask the question every 30 seconds instead. State the change.6 marks
    Answer
    1. (a) 30000 ms ÷ 1000 = 30, so `check` runs 30 seconds after the page loads [1], once only [1].
    2. (b) FALSE [1], a Boolean [1]; the ternary operator picks the value after `:`, so the alert shows Keep going [1].
    3. (c) Replace `setTimeout` with `setInterval`, so `check` runs repeatedly every 30 seconds [1].

    Trap: Saying both just delay the code, as examiners reported: `setTimeout()` runs once, `setInterval()` repeats.

  10. A page has `<p id="out"></p>` and this script. The user enters Ada, then Lovelace. ``` const first = prompt("Given name?"); const last = prompt("Family name?"); document.getElementById("out").innerHTML = "last" + first; ``` (a) State what the page displays and explain why. (b) The page should display the given name, a space, then the family name. Write the corrected last line. (c) Another programmer writes the last line as `document.getElementById("out").innerHTML = "Hello "dear" " + first;`. Explain what happens when the page runs.8 marks
    Answer
    1. (a) lastAda [1]: `"last"` is in quotes, so it is text, not the variable, and it is joined to `first` with no space [1].
    2. (b) `document.getElementById("out").innerHTML = first + " " + last;` No quotes around the variables [1]; `first` before `last` [1]; `" "` inserts the space [1].
    3. (c) The same type of quotes is used inside the string [1], so the code stops executing [1] and the string is not displayed [1].

    Trap: Quotes decide what is text: examiners reported misused double quotes stopping the code, and quotes around a variable name turn it into text.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
An HTML event is when the user does something.An HTML event is a change in a web page caused by a user or by the browser.
onmouseout starts when the pointer is ‘not over’ the element.onmouseout starts when the pointer moves away from the element.
console.log() outputs data for display in the browser.console.log() writes messages into the browser console, for testing or debugging.
setTimeout() and setInterval() both delay the start of the code.setTimeout() runs code once after a delay; setInterval() runs it repeatedly at set intervals.

Frequently asked questions

What is an HTML event in JavaScript?
An HTML event is a change in a web page caused by a user or by the browser, and JavaScript can start when it happens. The onmouseover event starts a script when the mouse pointer moves over an element, and onmouseout starts it when the pointer moves away from the element. The onload event starts a script when the page has finished loading into the browser.
What is the difference between prompt() and confirm() in JavaScript?
The prompt() method shows an input box where the user types a value and clicks OK or Cancel, and the value is returned to a variable, or NULL if the user cancels. The confirm() method shows OK and Cancel options and returns a Boolean: TRUE for OK and FALSE for Cancel. Both boxes block the script until the user responds.
What is the difference between setTimeout() and setInterval()?
setTimeout() runs code once, after a delay. setInterval() runs code repeatedly at set intervals. Both need two parameters and measure time in milliseconds. The difference is how often the code runs, not whether it is delayed.
What is the difference between === and !== in JavaScript?
=== returns TRUE only if both the value and the type are equal. !== returns TRUE if the value is not equal or the type is not equal. Because the type is compared as well as the value, the two operators can give different results for the same values.
How do you hide an element in JavaScript without leaving a gap?
Set the display style of the element to "none", using document.getElementById(id).style.display = "none". This hides the whole element with no space left where it was. Setting visibility to "hidden" also hides the element, but it leaves a blank space.
What is a common mistake in JavaScript exam answers about HTML events?
A common error in all parts was to reword the question, which is in effect repeating the question and does not gain credit. Another error was to say onmouseout starts when the pointer is ‘not over’ the element. It starts when the pointer moves away from the element.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters