Equivalent of include() in HTML
Solution 1
It cannot be done purely by HTML. (There are iframes, however, but I don't think that qualifies in this case.)
It can be done using JavaScript. You get the other file via Ajax, and place its contents inside an HTML element on the current page.
Solution 2
Have you tried:
<object type="text/html" data="file.html"></object>
Solution 3
Shameless plug of a library that I wrote the solve this.
https://github.com/LexmarkWeb/csi.js
<div data-include="/path/to/include.html"></div>
The above will take the contents of /path/to/include.html
and replace the div
with it.
Solution 4
HTML does not have a feature to include additional content natively. However most web servers do have server-side include statements:
SSI in Apache
SSI in IIS
Solution 5
the only thing would be an iframe which is pure html. but you also can use javascript to get the page via ajax and include it into your dom hirarchy
Related videos on Youtube
Trufa
Existing code exerts a powerful influence. Its very presence argues that it is both correct and necessary.
Updated on July 09, 2022Comments
-
Trufa almost 2 years
I was wondering wether there is a way to include some html content inside another html using only html?
A replacement to PHP's
<?php include("file.php"); ?>
Is this possible?
EDIT:
This has brought up some confusion, what I needed was "almost an html tag" that had the functionality of including a html document in another.
-
waldyrious over 9 years@Trufa I believe he's talking about the HTML5
seamless
attribute for iframes (html.spec.whatwg.org/#attr-iframe-seamless), but at the moment the support for it is pretty dismal: caniuse.com/#feat=iframe-seamless -
user over 7 years
iframe seamless
was removed from the spec. What a pity.
-
-
prodigitalson over 13 yearswell no, but it doesnt require something like PHP either.
-
Tyler Treat over 13 yearsThis is a server-side include, not plain HTML. Just as easy, if not easier, to use JavaScript probably.
-
prodigitalson over 13 years@tyler: except for then youre moving client side and generating a bunch of extra requests...
-
Your Common Sense over 13 yearsbut it does require something else! why not to use PHP then?
-
prodigitalson over 13 years@Col Shrapel: Well i would jsut use php as well, but the point is this is essentially built in to Apache and IIS so it doesnt require a 3rd party module being installed. That would be the only reason. But really, who DOESNT have PHP isntalled? It makes no sense to me either... but i didnt ask the question :-)
-
Tyler Treat over 13 years@Col. Shrapnel: I was going under the assumption OP was trying to avoid a server-side solution for whatever reason. If I were in his position of course I'd use PHP.
-
Trufa over 13 yearsI am very limited, I am actually asking for fmbl tab pages in facebook.
-
Joe Dargie almost 11 yearsOne drawback is that users without JavaScript enabled won’t see content that’s written out like that. That currently includes search engine crawlers, so the content won’t show up in search engines.
-
Cavemanharris almost 11 yearsOkay... thnx didnt think about that ! t
-
EML over 9 yearsDon't know why this has got so many downvotes. It's as good as most of the solutions here.
-
Darren Ringer almost 9 yearsVery nice but has at least one pitfall I found: at least in chrome, clicking a link inside the object frame will by default open the new link INSIDE the object, rather than in the browser. Setting link targets to target="_top" in the included file is one possible workaround.
-
skywalker almost 8 years"but it does require something else" It requires apache which is required by PHP. PHP adds even more requirements.
-
Ayoub Laazazi over 5 yearsAnother remark is that you need to call the JS and CSS files twice, in the main page and in the called html file
-
Brett Slocum almost 4 yearsWhat if the HTML file is on another server within the firewall? In other words, a content server controlled by us.
-
Server Overflow about 3 yearsThanks. Looks like the ultimate solution! BUT I get an error from Firefox, when loading the file locally (not on FTP): Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:////_topmenu2.html. (Reason: CORS request not http).