26
votes

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.

11
@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-seamlesswaldyrious
iframe seamless was removed from the spec. What a pity.user

11 Answers

21
votes

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.

35
votes

Have you tried:

<object type="text/html" data="file.html"></object>
13
votes

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.

7
votes

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

6
votes

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

3
votes

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

3
votes

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" -->
3
votes

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

2
votes

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 !

2
votes

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" );
0
votes

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!