How does client-side JavaScript make a web page interactive and validate user input?
Implement client-side interactivity with JavaScript, using variables, functions, events, DOM manipulation and form validation.
A focused answer to the SQA Advanced Higher Computing Science content on client-side JavaScript, covering variables and functions, events, manipulating the DOM, and validating form input in the browser.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
The SQA wants you to add interactivity to a web page with JavaScript that runs in the browser: respond to user events, read and change the page through the DOM, and validate form input before it is sent. This is client-side scripting, distinct from the server-side code that runs on the web server.
Client-side scripting
JavaScript runs on the user's machine inside the browser, so it can react instantly to what the user does without contacting the server. This makes it ideal for interface behaviour: responding to clicks, updating part of a page, and checking a form before it is sent. It is the third layer of a web page alongside HTML (content) and CSS (presentation): JavaScript adds behaviour.
Variables, functions and events
JavaScript declares variables and groups reusable instructions into functions, as any language does. The distinctive part is the event model: code is run in response to events. You attach a function (an event handler) to an element's event, such as a button's click or the document loading, and the browser calls your function when that event fires.
Manipulating the DOM
The Document Object Model is the browser's representation of the page as a tree of objects, one per element. A script reaches an element (for example by its id), then reads or changes it: altering text, changing a style, showing or hiding it, or reading the value the user typed. This is how JavaScript updates a page after it has loaded, without a full reload.
Form validation
Client-side validation uses JavaScript to check a form's fields before submission: that a required field is not empty, that a number is in range, or that an email has the right shape. If a check fails, the script displays a message and prevents submission, giving the user immediate feedback. Because a user can disable JavaScript, this validation improves usability but is not a security control: the server must validate again.
Try this
Q1. State what the DOM lets a JavaScript program do. [1 mark]
- Cue. Read and change the elements of the page as objects, updating what is displayed.
Q2. Give one limitation of client-side form validation. [1 mark]
- Cue. It can be bypassed (for example by disabling JavaScript), so the server must validate the data as well.
Exam-style practice questions
Practice questions written in the style of SQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AH style: client-side validation4 marksA registration form uses JavaScript to check the user has entered an email address before the form is sent. Explain how client-side validation works here and give one benefit and one limitation.Show worked answer →
Client-side validation runs JavaScript in the browser when the user submits (or leaves a field), reading the field's value and checking it against a rule, here that the email field is not empty and contains an at sign (1 mark); if the check fails, the script shows a message and stops the form being sent (1 mark). Benefit: it gives the user instant feedback without a round trip to the server, which is faster and reduces server load (1 mark). Limitation: it can be bypassed (a user can disable JavaScript), so the server must still validate the data; client-side validation is for usability, not security (1 mark).
AH style: events and DOM3 marksExplain what an event is in JavaScript and how the DOM is used to respond to one.Show worked answer →
An event is something that happens in the page that the script can react to, such as a click, a key press, or the page loading (1 mark). The DOM (Document Object Model) is the browser's object representation of the page, where each element is an object the script can read and change (1 mark). To respond, the script attaches a function to an element's event (for example onclick), and that function uses the DOM to read or update elements, for example changing text or showing an error (1 mark).
Related dot points
- Implement a web page using HTML5 structural and semantic elements and style it with CSS, using internal and external stylesheets, selectors, classes and IDs, the box model and responsive design.
A focused answer to the SQA Advanced Higher Computing Science content on HTML and CSS, covering HTML5 structural and semantic elements, CSS selectors, classes and IDs, internal and external stylesheets, the box model and responsive design.
- Implement server-side functionality with PHP, handling form data with GET and POST, using sessions, and connecting to a database to run SQL queries from a web page.
A focused answer to the SQA Advanced Higher Computing Science content on server-side scripting, covering PHP, handling form data with GET and POST, sessions, and connecting a web page to a database to run SQL.
- Analyse the requirements of a website and design it using a site structure diagram, wireframes, low-fidelity prototypes and user-centred design principles.
A focused answer to the SQA Advanced Higher Computing Science content on web analysis and design, covering functional requirements, site structure diagrams, wireframes, low-fidelity prototypes and user-centred design.
- Test a website for functional correctness, and evaluate it for usability, accessibility and fitness for purpose.
A focused answer to the SQA Advanced Higher Computing Science content on web testing and evaluation, covering functional testing against requirements, usability, accessibility and fitness for purpose.