Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 include file [duplicate]

Tags:

html

Use the object tag:

<object name="foo" type="text/html" data="foo.inc"></object>

foo.inc should include valid HTML.

Note: do NOT use the self-closing <object/> style. That will prevent content after the tag from being displayed.


<object> works fine, although it is not a self-closing tag, so it should look like this (otherwise it's not valid HTML5):

<object name="foo" type="text/html" data="foo.inc"></object>

<embed> can also be used for including external html content (and any MIME type):

<embed type="text/html" src="foo.inc">

For HTML5 you can try:

<head>
    <link rel="import" href="/path/to/imports/stuff.html">
</head>

(https://www.html5rocks.com/en/tutorials/webcomponents/imports/)

If this feature is not implemented in a target browser you can use webcomponents polyfill: http://webcomponents.org/polyfills/


The correct way is to use server side includes. The PHP solution requires PHP to be installed just for including files, the html solutions are not supported by the spec even though they work (the spec doesn't discriminate between mime types, but it does specify its intention, which means it could be explicitly prevented in the future, if those tags aren't just entirely deprecated).

Here's my post from a similar thread asking about the same thing:


If your server supports SSI (server side includes) you can put the following in your html-files without needing a scripting language or whatever. Apache has SSI enabled by default (I think?)

<!--#include file="same_path_file.html" -->
<!--#include virtual="docroot_file.html" -->

"file" is relative to the current file, and probably what you would use for including related files like "relevant_article_poll.html".

"virtual" is relative to document root (ie, your website root) and you would use it for including global files, like headers and footers.

Doesn't really matter which one you choose, but it's useful to know the difference between the two.

Also, the include directive makes a new internal http request to the server for each file, so you could include php files and the likes and they would be executed as they should.

Here's a useful overview of SSI: http://en.wikipedia.org/wiki/Server_Side_Includes


If you're writing pure HTML, include files aren't possible. However, you can use Apache's server-side includes (SSI) feature, or you can use some scripting language (Python, Ruby, PHP, etc.) to assemble the pages.