Live data from Hacker News

Why can't HTML alone do includes?

frontendmasters.com

91–100 of 367 posts

Re: Why can't HTML alone do includes?

#91

You could start with something like this: customElements.define('html-import', class extends HTMLElement { connectedCallback() { const href = this.getAttribute('href') const fetch = new XMLHttpRequest() fetch.responseType = 'document' fetch.addEventListener('readystatechange', (function onfetch(e) { if (fetch.readyState !== XMLHttpRequest.DONE) return const document = fetch.response.querySelector('body') ?? fetch.res…

Well, that's not "HTML alone".

Re: Why can't HTML alone do includes?

#92
post #17

This was the rabbit hole that I started down in the late 90s and still haven’t come out of. I was the webmaster of the Analog Science Fiction website and I was building tons of static pages, each with the same header and side bar. It drove me nuts. So I did some research and found out about Apache server side includes. Woo hoo! Keeping it DRY (before I knew DRY was a thing). Yeah, we’ve been solving this over and ove…

HTML is a markup language, not a programming language. It's like asking why Markdown can't handle includes. Some Markdown editors support them (just like some server-side tools do for HTML), but not all.

I think this is the most likely answer.

I'm not defending it, because when I started web development this was one of the first problems I ran into as well -- how the heck do you include a common header.

But the original concept of HTML was standalone documents, not websites with reusable components like headers and footers and navbars.

That being said, I still don't understand why then the frames monstrosity was invented, rather than a basic include. To save on bandwidth or something?

Re: Why can't HTML alone do includes?

#93

If I really need HTML includes for some reason, I'd reach for XSLT. I know its old, and barely maintained at best, but that was the layer intentionally added to add programming language features to the markup language that is HTML.

I believe XSLT 1 is still working in all major browsers today. Here's a simple HTML 5 example with two pages sharing a header template: https://gist.github.com/MarkTiedemann/0e6d36c337159a3e6d5072...

Re: Why can't HTML alone do includes?

#94
post #34

"Includes" functionality is considered to be server-side, i.e. handled outside of the web browser. HTML is client-side, and really just a markup syntax, not a programming language. As the article says, the problem is a solved one. The "includes" issue is how every web design student learns about PHP. In most CMSes, "includes" become "template partials" and are one of the first things explained in the documentation. T…

This argument applies just as much to CSS and JS. Why do they include "includes" when you can just bundle on the server?

For caching and sharing resources across the whole site, I suppose.

Re: Why can't HTML alone do includes?

#95
post #33

> We’ve got , which technically is a pure HTML solution, but they are bad for overall performance, accessibility, and generally extremely awkward here What does this mean? This is a pure HTML solution, not just "technically" but in reality. (And before iframe there were frames and frameset). Just because the author doesn't like them don't make them non-existent.

The real problem with iframes is that their size is set by the parent document only. They would be a lot more useful if we could write e.g. so the height of the iframe element is set by the abc.html document instead of the parent document.

You could do this with js in the child document, if its important to keep js out of the parent.

Re: Why can't HTML alone do includes?

#96
post #17

This was the rabbit hole that I started down in the late 90s and still haven’t come out of. I was the webmaster of the Analog Science Fiction website and I was building tons of static pages, each with the same header and side bar. It drove me nuts. So I did some research and found out about Apache server side includes. Woo hoo! Keeping it DRY (before I knew DRY was a thing). Yeah, we’ve been solving this over and ove…

HTML is a markup language, not a programming language. It's like asking why Markdown can't handle includes. Some Markdown editors support them (just like some server-side tools do for HTML), but not all.

Well, asciidoc - a markup language supports includes, so the "markup languages" analogy doesn't hold.

https://docs.asciidoctor.org/asciidoc/latest/directives/incl...

Re: Why can't HTML alone do includes?

#97

Earlier quoted context omitted.

HTML is a markup language, not a programming language. It's like asking why Markdown can't handle includes. Some Markdown editors support them (just like some server-side tools do for HTML), but not all.

That’s the Hyper part of HTML, and what makes it special. It’s made to pull in external resources (as opposed to other document formats like PDF). Scripts, stylesheets, images, objects, favicons, etc. HTML is thematically similar.

No, HTML is fundamentally different because (for a static site without any JS dom manipulation) it has all the semantic content, while stylesheets, images, objects, etc. are just about presentation.

Re: Why can't HTML alone do includes?

#98
post #9

Earlier quoted context omitted.

Iframe is stuck in a rectangular box. It's not really suitable for things like site wide headers, footers and menus.

While I get your point, headers and footers and menus tend to all live within rectangular boxes.

Headers and footers, yes. Menus generally need to expand when you interact with them, especially on mobile.

Re: Why can't HTML alone do includes?

#99

I'm not an expert on this but IMO, from a language point of view, HTML is a markup language, it 'must' have no logic or processing. It is there to structure the information not to dynamically change it. Nor even to display it nicely. The logic is performed elsewhere. If you were to have includes directly in HTML, it means that browsers must implement logic for HTML. So it is not 'just' a parser anymore. Imagine for e…

There is a very, very broad line in that "no logic or processing". HTML/CSS already do a lot of logic and processing. And many "markup languages" have include support. Like wikitext used in wikipedia and includes in Asciidoc.

Re: Why can't HTML alone do includes?

#100
post #96

Earlier quoted context omitted.

HTML is a markup language, not a programming language. It's like asking why Markdown can't handle includes. Some Markdown editors support them (just like some server-side tools do for HTML), but not all.

Well, asciidoc - a markup language supports includes, so the "markup languages" analogy doesn't hold. https://docs.asciidoctor.org/asciidoc/latest/directives/incl...

[deleted]
Post reply on HN