Earlier quoted context omitted.
It's too bad we didn't go down the XHTML/semantic web route twenty years ago. Strict documents, reusable types, microformats, etc. would have put search into the hands of the masses rather than kept it in Google's unique domain. The web would have been more composible and P2P. We'd have been able to slurp first class article content, comments, contact details, factual information, addresses, etc., and built a wealth…
The semantic web is a silly dream of the 90s and 00s. It's not a realizabile technology, and Google basically showed exactly why: as soon as you have a fixed algorithm for finding pages on the web, people will start gaming that algorithm to prioritize their content over others'. And I'm not talking about malicious actors trying to publish malware, but about every single publisher that has theoney to invest in figurin…
Why can't HTML alone do includes?
351–360 of 367 posts
Re: Why can't HTML alone do includes?
#352Earlier quoted context omitted.
> Why not have a simple client side method for this? Like writing a line of js?
A line of JS that has to run through the Javascript interpreter in your browser rather than a simple I/O operation? If internally this gets optimized to a simple I/O operation (which it should) then why add the JS indirection in the first place?
That’s the reason it doesn’t get implemented. Nobody wants the simple I/O operation based inclusion. The moment you try to propose it, there’ll be demands to add conditional logic or macros. More relevant for the web is instantiating html templates, which will undoubtedly get piled onto such a feature. And pretty soon you have yet another:
> interpreter in your browser
Might as well use the one already there.
Re: Why can't HTML alone do includes?
#353Earlier quoted context omitted.
Seems like overkill to bring in a framework just for inlining some static html. If that's all you're doing, a self-replacing script tag is neat: function includeHTML(url) { const s = document.currentScript fetch(url).then(r => r.text()).then(h => { s.insertAdjacentHTML('beforebegin', h) s.remove() }) } ... includeHTML('/footer.html') The `script` element is replaced with the html from `/footer.html`.
this here is the main idea of HTMX - extended to work for any tag p, div, content, aside … there are many examples of HTMX (since it is a self contained and tiny) being used alongside existing frameworks of course for some of us, since HTMX brings dynamic UX to back end frameworks, it is a way of life https://harcstack.org (warning - raku code may hurt your eyes)
Re: Why can't HTML alone do includes?
#354This is the closest we can do today: -- index.html Hello includes -- header.js document.currentScript.outerHTML = ` Header ` -- footer.js document.currentScript.outerHTML = ` Footer ` Scripts will replace their tags with html producing a clean source, not pretty but it works on the client
Hello includes
(async function include(){
const objs = document.getElementsByTagName('include')
const tags = Array.from(objs)
for(tag of tags){
const src = tag.getAttribute('src')
const data = await fetch(src)
const html = await data.text()
tag.outerHTML = html
}
})()
* beware autoclosing tags won't workRe: Why can't HTML alone do includes?
#355Earlier quoted context omitted.
Me personally, I didn't even care that much about strict semantic web, but XML has the benefits of the entire ecosystem around it (like XPath and XSLT), composable extensibility in form of namespaces etc. It was very frustrating to see all that thrown out with HTML5, and the reasoning never made any sense to me (backwards compatibility with pre-XHTML pages would be best handled by defining a spec according to which t…
If XHTML was literally just HTML but with XML syntax, it would be pretty cool.
Re: Why can't HTML alone do includes?
#356Earlier quoted context omitted.
It is.
XHTML 1.0 was, and they evolved it incompatibly.
Re: Why can't HTML alone do includes?
#357Earlier quoted context omitted.
Don't Service Workers API provide this now, essentially act like a in-browser proxy to the server. https://developer.mozilla.org/en-US/docs/Web/API/Service_Wor...
Rational or not, some of us try very hard to avoid JavaScript based solutions.
Re: Why can't HTML alone do includes?
#358Earlier quoted context omitted.
A block of in-line JavaScript stops the renderer until it runs because its output cannot be determined before it completes.
So would any form of html inclusion.
Re: Why can't HTML alone do includes?
#359We almost could. Chrome shipped a draft of HTML Imports back in 2014. You’d do exactly that, the browser would fetch the fragment, parse it, and make it available for insertion. The idea died for three reasons that still apply today:
Execution‑order and performance hazards. Images, scripts, and styles are fire‑and‑forget: the preload scanner sees a URL, starts the fetch, and the parser keeps streaming. With HTML fragments you need the full subtree before you can finish parsing the parent document (otherwise IDs, custom‑element upgrades, , etc. fire in the wrong order). That either stalls the parser—horrible for TTFB—or forces async insertion, which produces layout shifts. Everyone hated both outcomes.
Security and isolation. If an imported fragment can run scripts it becomes an XSS foot‑gun; if it can’t run scripts it breaks a surprising amount of markup (think onerror, custom elements with module scripts, CSP inheritance, etc.). The platform already has an “HTML that can’t run scripts” container: it’s called an iframe. Anything more permissive lands in a swamp of half‑trusted execution.
The “circular dependency” tar‑pit. Templates inherit CSS scopes, custom element registries, and base URLs from the document that instantiates them. Once you let HTML pull in more HTML, those scopes can nest arbitrarily—and can link back to parents. The HTML spec team tried to spec out the edge‑cases and basically threw up their hands. (There’s a famous TAG thread titled “HTML Imports considered harmful” that reads like war diaries.)
Meanwhile developers solved the “shared header” problem higher up the stack—SSI, PHP include, SSG partials, React components, you name it—so browser vendors didn’t see a payoff big enough to justify the complexity. The attitude became: “composition is a build‑time concern, not a runtime primitive.”
Could it ever come back? Maybe, but the bar is higher now that everyone has a build step. A proposal would need to:
Stream (no parser‑blocking)
Sandbox (no ambient script execution)
Deduplicate (avoid circular fetch hell)
Play nicely with CSP, SRI, origin isolation, and the module graph
That starts to look a lot like… , which we already have—just not the ergonomic sugar we wish for.
So the short answer is: HTML includes are easy in user‑land but devilishly hard to make safe, fast, and spec‑compliant in the browser itself.
Re: Why can't HTML alone do includes?
#360Earlier quoted context omitted.
You can achieve that with js in the parent document.
You can achieve everything with JS in the parent document, it doesn’t mean it should be required or even recommended