Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 31 / 31THE BROWSER

Trust has boundaries.

The browser is public; secrets and authority belong on the server.

01 / THE BASICS

Start here.

A program receives data from places it does not control: forms, URLs, servers, and other scripts. Treat that data as values to inspect before relying on it. A string that looks like a number may fail conversion, an object may lack an expected property, and a response may contain text you would not want interpreted as page markup. A trust boundary is the moment external data enters code that makes assumptions about its shape or meaning.

In a browser, assigning untrusted text to textContent displays it as text. Assigning it to innerHTML asks the browser to parse it as HTML, which can create elements and executable behavior. Use textContent for ordinary user-provided words. When markup is genuinely required, use a trusted rendering path with appropriate sanitization and clear rules about what is allowed. The exact same string is safe or dangerous depending on where it is placed.

Validation and output handling solve different problems. Validation checks whether input fits the program's rules, such as a price being a finite nonnegative number. Safe output handling controls how data is interpreted when displayed. Neither should be left to the user's good intentions. A reliable JavaScript program makes both boundaries explicit, reports recoverable problems clearly, and avoids silently treating malformed input as valid data.

02 / THE IDEA

Build the model.

Browser code runs on a visitor's device and can be inspected or changed. It cannot keep a private API key. A server should decide what a request may do, validate its data, and use secrets only in the server environment. A hidden form field can discourage simple bots but is not authorization.

Cross-site scripting happens when untrusted text becomes executable markup or script. Prefer APIs that insert text as text, and keep data and HTML separate. Security is not one special library call; it is a habit of asking who supplied a value and what power the next step gives it.

03 / UNDER THE SURFACE

How it works.

Anything shipped to the browser is available to the visitor. A secret API key placed in client JavaScript can be read from the downloaded code or network requests. Keep secrets and privileged operations on a server. The browser sends a request describing what it wants; the server validates the request and decides whether to perform it. A disabled button only changes the interface and does not stop direct requests.

Untrusted data can also cross a rendering boundary. textContent displays characters as text. innerHTML parses a string as markup, so a visitor-supplied value may create tags or event handlers if inserted carelessly. Treat URL parameters, form fields, and remote responses as untrusted until validated or safely rendered. Security rules belong at each boundary where data gains more power.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const messageNode = document.createElement('p');
const visitorMessage = '<b>Hello</b>';
messageNode.textContent = visitorMessage;
document.body.append(messageNode);

First notice. Text insertion displays the value without interpreting markup in it.

createElement makes a paragraph that is not yet on the page. visitorMessage contains characters that look like HTML markup. Assigning it to textContent makes those characters plain text, so the browser does not create a bold element. append adds the paragraph to the document. The value is still untrusted for other uses, but this display operation does not grant it HTML interpretation.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const message = '<b>Hello</b>';
const output = document.createElement('div');
output.textContent = message;
document.body.append(output);

The page displays the literal characters <b>Hello</b>, not bold Hello. textContent is the correct operation for plain visitor text. If trusted formatting is required, build known elements explicitly or use a carefully reviewed sanitization process; do not simply swap in innerHTML.

06 / KEEP THIS

One thing to remember.

Assume client input is changeable; keep secrets and final decisions on the server.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.