Static HTML and server rendering → Dynamic HTML and the browser wars
1991 – today · 1995 – today
What changed
- Where the state lives
Static HTML and server rendering
On the server: the todo list is server data; the current filter is in the URL and in hidden form fields.
Dynamic HTML and the browser wars
In the page: a JavaScript array and a filter variable. Nothing is saved; a reload starts again from the code.
- How the page is rendered
Static HTML and server rendering
The server writes the whole page as an HTML string for every request; the browser only displays it.
Dynamic HTML and the browser wars
render()builds the whole list as one HTML string and swaps it in withinnerHTML.- What happens on a change
Static HTML and server rendering
Every action is a request: a form POST or a link GET, answered by a complete new page.
Dynamic HTML and the browser wars
Handler attributes (
onsubmit,onclick) call global functions that change the array and callrender().- How the code is organised
Static HTML and server rendering
One server program that routes requests and renders pages. No JavaScript is needed in the browser.
Dynamic HTML and the browser wars
One script of global functions, wired to the page by attributes in the HTML. No server is involved.
Why it changed
What hurt in each era along the way, and the era that came next.
Static HTML and server rendering→Dynamic HTML and the browser wars
Every change, even ticking one checkbox, is a round trip to the server and a new page: the screen flashes, the scroll position and anything half-typed are lost, and the user waits for the network each time. Pages could not react to the user on their own. JavaScript (1995) and Dynamic HTML let the page change itself in the browser.
The same app, side by side
The main file of each version. Both apps pass the same test: same markup, same behaviour, different ideas.
The apps run in sandboxes in your browser.
Static HTML and server rendering
plain browser APIs, no library
/server.js · 83 lines
// Static HTML era: the server builds every page; the browser shows it and sends forms back.
// In the 1990s this was a CGI script (often Perl, later PHP); here it is JavaScript.
// The "database" lives on the server, so the list survives every page load.
const todos = [
{ id: 1, title: "Read the docs", done: true },
{ id: 2, title: "Write the app", done: false },
];
let nextId = 3;
const FILTERS = { all: "All", active: "Active", done: "Done" };
// Text typed by users must be escaped before it goes into the HTML.
function escapeHtml(text) {
return String(text)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
function renderPage(filter) {
const shown = todos.filter((t) => filter === "all" || (filter === "done") === t.done);
const left = todos.filter((t) => !t.done).length;
// Every todo gets its own little form: toggling it is a POST to the server.
const items = shown
.map(
(t) => `
<li${t.done ? ' class="done"' : ""}>
<form method="post" action="/todos/${t.id}/toggle">
<input type="hidden" name="filter" value="${filter}">
<button type="submit" data-toggle>${t.done ? "Undo" : "Done"}</button>
</form>
<span class="title">${escapeHtml(t.title)}</span>
</li>`,
)
.join("");
// Filters are plain links: choosing one loads a new page.
const links = Object.entries(FILTERS)
.map(
([name, label]) =>
`<a href="/?filter=${name}" data-filter="${name}"${name === filter ? ' class="selected"' : ""}>${label}</a>`,
)
.join(" ");
return `
<main class="todo-app">
<h1>Todos</h1>
<form id="todo-form" method="post" action="/todos">
<input type="hidden" name="filter" value="${filter}">
<input id="new-todo" name="title" placeholder="What needs doing?" autocomplete="off" aria-label="New todo">
<button id="add" type="submit">Add</button>
</form>
<ul id="todo-list">${items}
</ul>
<div class="footer">
<span id="todo-count">${left} left</span>
<div class="filters">${links}</div>
</div>
</main>`;
}
// One request in, one whole page out. The answer to a POST is the new page itself.
export function handle({ method, path, query, form }) {
const asked = form?.filter ?? query.filter;
const filter = Object.hasOwn(FILTERS, asked ?? "") ? asked : "all";
if (method === "POST" && path === "/todos") {
const title = (form.title ?? "").trim();
if (title) todos.push({ id: nextId++, title, done: false });
}
const toggle = path.match(/^\/todos\/(\d+)\/toggle$/);
if (method === "POST" && toggle) {
const todo = todos.find((t) => t.id === Number(toggle[1]));
if (todo) todo.done = !todo.done;
}
if (path === "/" || method === "POST") return { status: 200, body: renderPage(filter) };
return { status: 404, body: "<h1>Not found</h1>" };
}Dynamic HTML and the browser wars
plain browser APIs, no library
/src/main.js · 73 lines
// DHTML era: the page changes itself in the browser. The list is a JavaScript array, and
// render() writes it into the page as one HTML string. No server is asked.
// The state is plain variables in the page: a reload starts again from these lines.
var todos = [
{ id: 1, title: "Read the docs", done: true },
{ id: 2, title: "Write the app", done: false }
];
var nextId = 3;
var filter = "all";
var FILTERS = ["all", "active", "done"];
// The text the user typed becomes part of the HTML, so it is escaped first.
function escapeHtml(text) {
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
function render() {
var html = "";
var left = 0;
var i, todo;
for (i = 0; i < todos.length; i++) {
todo = todos[i];
if (!todo.done) left++;
if (filter == "all" || (filter == "done") == todo.done) {
// The handler is part of the string: onclick calls a global function with the id.
html += '<li' + (todo.done ? ' class="done"' : '') + '><label>' +
'<input type="checkbox" data-toggle onclick="toggleTodo(' + todo.id + ')"' +
(todo.done ? ' checked' : '') + '> ' +
'<span class="title">' + escapeHtml(todo.title) + '</span></label></li>';
}
}
// innerHTML throws the old items away and builds new ones, so a focused checkbox loses focus.
document.getElementById("todo-list").innerHTML = html;
document.getElementById("todo-count").innerHTML = left + " left";
for (i = 0; i < FILTERS.length; i++) {
document.getElementById("filter-" + FILTERS[i]).className = FILTERS[i] == filter ? "selected" : "";
}
}
function addTodo() {
var field = document.getElementById("new-todo");
var title = field.value.replace(/^\s+|\s+$/g, "");
if (title != "") {
todos.push({ id: nextId++, title: title, done: false });
field.value = "";
render();
}
// false cancels what the browser would do next: send the form and load a new page.
return false;
}
function toggleTodo(id) {
for (var i = 0; i < todos.length; i++) {
if (todos[i].id == id) todos[i].done = !todos[i].done;
}
render();
}
function setFilter(name) {
filter = name;
render();
}
// A <script> of the time ran in the global scope: every function above became global, and
// the onclick attributes could call it. The lab runs each file as a module with its own
// scope, so the functions the markup calls are put on window by hand.
window.addTodo = addTodo;
window.toggleTodo = toggleTodo;
window.setFilter = setFilter;
// Like a <script> at the end of <body>, this runs when the elements already exist: draw the list.
render();