Sir-ADBlogSIR-ADBLOGFraud awareness and web application security
Blog
GalleryTech NewsStart a project
Web & Engineering

The DOM and XSS Explained: How a Website Can Be Manipulated Even When It Looks Normal

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.

Sir Brown ADSir Brown ADNovember 4, 2026 6 min read
Add as Preferred Source
The DOM and XSS Explained: How a Website Can Be Manipulated Even When It Looks Normal
Share

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.

What Is the DOM?

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:

html
<h1 id="welcome">Welcome</h1>

JavaScript can find it and change it:

js
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.

Where the Danger Starts

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:

js
element.innerHTML = "Hello, " + name;

New Updates

The Importance of a Portfolio in Building Client Trust

The Importance of a Portfolio in Building Client Trust

Affordable Website Packages for Small Businesses and Solopreneurs

Affordable Website Packages for Small Businesses and Solopreneurs

How Sir Brown AD Builds Websites That Actually Convert

How Sir Brown AD Builds Websites That Actually Convert

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.

What XSS Means

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.

Why It Matters

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 Safe Way to Show Text

The fix is usually one word.

Unsafe:

js
element.innerHTML = userInput;

Safer:

js
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.

Hiding a Button Is Not Security

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:

js
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.

Questions a Careful Developer Asks

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?

A Safe Practice Exercise

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.

Conclusion

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.

Join my WhatsApp channel

Get new articles, fraud alerts and web security tips straight to your phone.

Join channel

Found this useful? Share it.

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 us
Sir Brown AD
Written by

Sir Brown AD

Full stack engineer and publisher, Abuja

I build secure web platforms for Nigeria and Africa, and write about fraud awareness, web security and what I learn shipping real products.

Book a call Full profile

Discussion

0 comments
No comments yet. Be the first.
On this page
Details
AuthorSir Brown AD
PublishedNovember 4, 2026
Read time6 min
TopicWeb & Engineering

Need a site or app?

I build secure platforms, payment flows and write sponsored articles.

Start a project

Keep reading, or tell me about what you're building.

SIR-AD BLOG

Fraud awareness. Web security. Built in Nigeria.

Get the weekly scam alerts

Start your week with the latest fraud warnings, security lessons and plain-language guides delivered straight to your phone or inbox.

Subscribe

Connect with us

WhatsApp channel

Abuja, Nigeria. Open to projects, remote worldwide.

Explore

PortfolioAboutBlogTech NewsGalleryJobsNewsletterFAQ

Work with me

ContactServicesGet a WebsiteAdvertiseSponsor

Platforms

Fraud Radar NG LAN Library TheCyclopedia News YotaPoint

Legal

PrivacyTermsSitemap

© 2026 Sir-AD Blog/ Sir Brown AD · Abuja, Nigeria