28

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.

Trufa
  • 39,971
  • 43
  • 126
  • 190
  • 4
    @Trufa I believe he's talking about the HTML5 `seamless` attribute for iframes (https://html.spec.whatwg.org/#attr-iframe-seamless), but at the moment the support for it is pretty dismal: http://caniuse.com/#feat=iframe-seamless – waldyrious Oct 25 '14 at 18:30
  • 2
    `iframe seamless` was [removed](https://github.com/whatwg/html/issues/331) from the spec. What a pity. – user Jan 11 '17 at 00:04

12 Answers12

39

Have you tried:

<object type="text/html" data="file.html"></object>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Victor
  • 3,520
  • 3
  • 38
  • 58
  • 3
    Very 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. – Darren Ringer May 28 '15 at 19:34
  • Another remark is that you need to call the JS and CSS files twice, in the main page and in the called html file – Ayoub Laazazi Aug 15 '18 at 14:12
22

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.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
14

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.

Michael Marr
  • 2,101
  • 14
  • 13
  • Thanks. 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). ​ – Gabriel Mar 04 '21 at 15:57
  • Same, I got a "XMLHttpRequest" error: "from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted." Nice try though and I'll continue testing. – jacktrader Dec 07 '22 at 15:20
7

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

Mr Griever
  • 4,014
  • 3
  • 23
  • 41
6

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

ITroubs
  • 11,094
  • 4
  • 27
  • 25
4

Yes there is but you need to enable it in your config or .htaccess:

Options +Includes
AddType text/html .shtml
AddHandler server-parsed .shtml

Of course with that youd need to rename any file doing the including to .shtml... or you could jsut use:

Options +Includes
AddType text/html .html
AddHandler server-parsed .html

the syntax itself is similar to comment:

<!--#include virtual="/footer.html" -->
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • well no, but it doesnt require something like PHP either. – prodigitalson Oct 13 '10 at 21:53
  • This is a server-side include, not plain HTML. Just as easy, if not easier, to use JavaScript probably. – Tyler Treat Oct 13 '10 at 21:54
  • @tyler: except for then youre moving client side and generating a bunch of extra requests... – prodigitalson Oct 13 '10 at 21:56
  • but it does require something else! why not to use PHP then? – Your Common Sense Oct 13 '10 at 21:56
  • 1
    @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 :-) – prodigitalson Oct 13 '10 at 21:59
  • @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. – Tyler Treat Oct 13 '10 at 22:00
  • "but it does require something else" It requires apache which is required by PHP. PHP adds even more requirements. – skywalker Jul 23 '16 at 13:55
3

There's no such thing. You'd have to use a server-side scripting language or JavaScript to do something like this.

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
3

If you're using Apache, you can try Server Side Includes.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
2

This might be a few years late but this is how i did it !

in the first line after put this line !

<SCRIPT LANGUAGE="JavaScript" src="http://yourdomain.com/header.js">

then create a file called "header.js" and enter the content of the file you want to include ! like so....

<!-- Begin
document.write('<center>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<hr>');
document.write('</center>');
// End -->

Hope this help !

Cavemanharris
  • 185
  • 2
  • 14
  • 2
    One 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. – Paul D. Waite Jul 06 '13 at 15:40
  • 1
    Okay... thnx didnt think about that ! t – Cavemanharris Jul 06 '13 at 15:41
  • Don't know why this has got so many downvotes. It's as good as most of the solutions here. – EML Sep 02 '14 at 09:54
2

Almost 10 years later, some people may still have doubts about this. So I'm going to explain a simple solution that we have today in 2020.

I always use the jquery .load() function and never had a problem with that.

Exemple: ( "#content" ).load( "includes/menu.html" );
Ray
  • 21
  • 2
0

The lack of Include\Import in Html is really frustrating!

A good alternative is "Server Side Includes (SSI)" in case "PHP" is not supported!

SSI is supported by almost any (if not all) web host server!

<!--#include virtual="layout.html" -->

The file that contains the above line must end with ".shtml" or ".shtm" extensions!


It's really annoying that something as easy as Include\Import can't be performed by the browser itself!

Like php or Node.js, preprocessing the html using Javascript itself before the HTML Load process start should be supported in any browser!

User
  • 71
  • 1
  • 7
0

Now in 2022 I use Vanilla Javascript fetch().

This is how to include file: "newfile.html" into div: "container"

// HTML
<div id="container"></div>


// JS
const loadHtmlFile = (fileName, containerId) => {
  fetch(fileName).then(function (response) {
    return response.text();
  }).then(function (html) {
    containerId.innerHTML = html;
  }).catch(function (err) {
    console.warn('Something went wrong.', err);
  });
}

loadHtmlFile("newfile.html", container)