Is it possible to include html documents within html documents?

Viewed 56

I'm doing just doing the front end of a website to put it on github and I was wondering if there is any way to import html files into another html file. I know I can use php, but I'm interested in whether it's possible to do it with html.

3 Answers

You can use an iframe:

The HTML Inline Frame element (<iframe>) represents a nested browsing context, embedding another HTML page into the current one.

The mechanism that was supposed to bring this to HTML is called HTML imports, but never made it into browsers except Chrome, and will be removed even from Chrome if it isn't already according to https://developer.mozilla.org/en-US/docs/Web/Web_Components/HTML_Imports.

Of course SGML (and by extension also XML), on which HTML is based, always had powerful ways to syntactically compose HTML from fragments. In SGML (and XML), you can declare named entities as text variables holding any text (either plain text or text containing markup). In particular, you can declare the replacement content for an entity to be pulled from an external file or http:// resource. There's a basic example for how to pull-in header and footer fragments given in my SGML tutorial right at the beginning (Producing HTML Tutorial).

Note however that this does not work in browsers. Even if you use XHTML and a custom DOCTYPE containing entity declarations, browsers won't pull-in/expand entity references.

In this case you would use JS, since it’s purely on the frontend. You would use PHP when you’re doing something similar on the backend.

Related