What is the best way - cross browser compatible to include an HTML file in HTML5.
I am building a site in HTML5 and would like to have the nav in one separate file and included in the site pages.
What is the best way - cross browser compatible to include an HTML file in HTML5.
I am building a site in HTML5 and would like to have the nav in one separate file and included in the site pages.
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.
Ok, these are all valid answers, but except the javascript method the others are quite limited.
The php one is strictly bound to the provider.
The object and the embed, instead, include the code without any style (except if you declare it in the included file).
So I mean that's not good enought as the php or asp include which works fine but needs a server that supports it.
I'm not aware of any way of doing it in HTML, but if you were to do it in PHP, it's simple enough, if you had a certain nav bar you wanted included in every page you'd just put:
<?php include('nav.php') ?>
Putting the HTML code in a seperate page for the nav, and the PHP would automatically slam it in there each time. And you don't have to know PHP to make PHP pages per se, you can just use general HTML markup in a PHP file and it'll work fine.
HTML5 is made out of 3 components: HTML, CSS and JavaScript. So we have to use all of them to take advantage of HTML5.
External HTML code can be included in another html document using javascript. The only thing is, you have to store external code in .js file. Here is an example how to include html paragraph:
<!DOCTYPE html>
<html>
<head>
<title>Main Page</title>
<script type="text/javascript" src="include_html.js"></script>
</head>
<body>
<script type="text/javascript">
paragraph();
</script>
</body>
</html>
Contents of include_html.js
function paragraph()
{
document.write("<p> This is an HTML paragraph </p>");
}