Web security is not only about passwords and firewalls. A lot of it happens inside your browser, where JavaScript changes what you see. Sir Brown AD explains the DOM and XSS in plain language, with simple examples and the one rule that prevents most of it.

Many people think web security is only about passwords, firewalls and antivirus. In reality, a big part of it happens inside the browser, where a website is displayed and where JavaScript can change what you see.
Two ideas explain a lot of that: the DOM and XSS. Once you understand them, you start to see how a website can be manipulated even when it looks completely normal.
The danger is not the browser. The danger is a website that treats a stranger's text as if it were its own code.
DOM stands for Document Object Model. It is the browser's live copy of a web page. When you open a site, the browser reads the HTML and turns it into a structure that JavaScript can read and change.
Take this heading:
<h1 id="welcome">Welcome</h1>JavaScript can find it and change it:
document.getElementById("welcome").textContent = "Welcome, Ada!";The page now says Welcome, Ada! The HTML file on the server did not change. Only the browser's live copy did. That live copy is the DOM.
Think of it as a family tree. The page is the top, with a head and a body below it, and inside the body are headings, paragraphs, buttons and images. JavaScript can read any of them, change their text, add new ones, remove old ones, and react when you click. This is why modern sites feel alive when you add to a basket or see a message without the page reloading.
The DOM is powerful because it lets a site react to people. It becomes dangerous when a site puts data it does not control into the page in the wrong way.
Imagine a welcome message built like this:
element.innerHTML = "Hello, " + name;If name is Ada, you get Hello, Ada. Harmless. But what if the name comes from a URL, a form or a comment, and someone controls it? If they send HTML instead of a name, such as an image tag with a script handler attached, then innerHTML does not show it as words. The browser reads it as real HTML and may run the code inside it.
XSS stands for Cross-Site Scripting. It is when an attacker gets their code to run inside another person's browser, on a website that person trusts. The server is often working perfectly. The mistake is that the page placed untrusted text into the DOM in a way the browser treated as code.
There is a simple example. A search page says You searched for, followed by the search term. If the code puts that term in with innerHTML, then a search for apple looks normal. A search for HTML with a script handler can run code, and anyone who clicks a link with that search inside is affected.
The visitor did nothing wrong. They clicked a link to a site they trust.
Code running inside a trusted page can do what the visitor can do on that page. It can change what they see, show a fake login box, read information shown on the page, or send requests as them. That is why a small mistake in how text is displayed can become a serious problem.
The fix is usually one word.
Unsafe:
element.innerHTML = userInput;Safer:
element.textContent = userInput;With textContent, whatever the person typed is shown as plain words. Even if it looks like HTML, the browser prints it as text and does not run it.
The same applies to a comment list. Building the page from a string of HTML puts every comment through the browser's HTML reader. Creating a paragraph element and setting its textContent keeps each comment as text. A bad comment then stays a bad comment, instead of becoming code that runs for every visitor.
A quick way to remember it:
User input plus innerHTML can mean code runs
User input plus textContent means it is shown as text
innerHTML is not always wrong. The risk appears when it is used with data a user can influence.
There is a second lesson hiding here. Anything in the browser can be controlled by the person using it. A visitor can open DevTools, read your JavaScript, change values, edit requests, or call your API directly without ever clicking your buttons.
So a line like this is not security:
if (user.role === "admin") {
showAdminPanel();
}It only hides a button. The server must still check who the user is before it returns admin data or accepts an admin action. Show what is allowed in the browser, but enforce what is allowed on the server.
Where does this value come from: a URL, a form, an API, a database or another user?
Can someone else control it? If yes, treat it as untrusted.
How will the browser treat it: as text, as HTML, as part of a link, or as code?
Does the server check it, and does it check the person's permission?
If you are learning, use a deliberately vulnerable practice app such as OWASP Juice Shop that you run yourself, and never test on a site you do not own or have permission to test.
Open the app and press F12 to open DevTools
In the Elements panel, find a heading or a button
In the Console, change its text safely, for example set a heading's textContent to Security Practice
Open the Network tab, refresh the page, and watch the requests
Ask what would happen on the server if someone changed those values
The lesson is that what you see is only the front end. Real security is about what the server accepts, checks and allows.
NEVER LET UNTRUSTED INPUT BECOME EXECUTABLE CODE.
The DOM is what makes websites interactive, and it is not the enemy. XSS appears when a site lets outside text be treated as HTML or JavaScript. Show user text as text, do not trust the browser to enforce your rules, and always validate and authorise on the server.
Once you see websites this way, you stop seeing only buttons and pages. You start seeing data moving between a browser and a server, and you start asking whether it is being handled safely.
Platforms like Sir-AD Blog will keep explaining web security in plain language, because understanding how attacks work is the best way to build things that resist them.
Get new Web & Engineering alerts
One email when a new web & engineering article like this one goes live. No spam, unsubscribe anytime.
Get new articles, fraud alerts and web security tips straight to your phone.
Someone you know may be about to fall for a scam or make a costly mistake. Send them this.
This article was originally written and published by Sir Brown AD.
Advertise with usKeep reading, or tell me about what you're building.
Discussion