I have 2 HTML files, suppose a.html
and b.html
. In a.html
I want to include b.html
.
In JSF I can do it like that:
<ui:include src="b.xhtml" />
It means that inside a.xhtml
file, I can include b.xhtml
.
How can we do it in *.html
file?
localhost
: stackoverflow.com/questions/7542872/…
In my opinion the best solution uses jQuery:
a.html
:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#includedContent").load("b.html");
});
</script>
</head>
<body>
<div id="includedContent"></div>
</body>
</html>
b.html
:
<p>This is my include file</p>
This method is a simple and clean solution to my problem.
The jQuery .load()
documentation is here.
Expanding lolo's answer, here is a little more automation if you have to include a lot of files. Use this JS code:
$(function () {
var includes = $('[data-include]')
$.each(includes, function () {
var file = 'views/' + $(this).data('include') + '.html'
$(this).load(file)
})
})
And then to include something in the html:
<div data-include="header"></div>
<div data-include="footer"></div>
Which would include the file views/header.html
and views/footer.html
.
data-argument
and retrieve it in the included file?
$("#postdiv").load('posts.php?name=Test&age=25');
My solution is similar to the one of lolo above. However, I insert the HTML code via JavaScript's document.write instead of using jQuery:
a.html:
<html>
<body>
<h1>Put your HTML content before insertion of b.js.</h1>
...
<script src="b.js"></script>
...
<p>And whatever content you want afterwards.</p>
</body>
</html>
b.js:
document.write('\
\
<h1>Add your HTML code here</h1>\
\
<p>Notice however, that you have to escape LF's with a '\', just like\
demonstrated in this code listing.\
</p>\
\
');
The reason for me against using jQuery is that jQuery.js is ~90kb in size, and I want to keep the amount of data to load as small as possible.
In order to get the properly escaped JavaScript file without much work, you can use the following sed command:
sed 's/\\/\\\\/g;s/^.*$/&\\/g;s/'\''/\\'\''/g' b.html > escapedB.html
Or just use the following handy bash script published as a Gist on Github, that automates all necessary work, converting b.html
to b.js
: https://gist.github.com/Tafkadasoh/334881e18cbb7fc2a5c033bfa03f6ee6
Credits to Greg Minshall for the improved sed command that also escapes back slashes and single quotes, which my original sed command did not consider.
Alternatively for browsers that support template literals the following also works:
b.js:
document.write(`
<h1>Add your HTML code here</h1>
<p>Notice, you do not have to escape LF's with a '\',
like demonstrated in the above code listing.
</p>
`);
'
marks in it. If you just do a find / replace to replace ` with
\` THEN find / replace to replace '
with \'
and new-lines with ``new-lines it will work fine.
Checkout HTML5 imports via Html5rocks tutorial and at polymer-project
For example:
<head>
<link rel="import" href="/path/to/imports/stuff.html">
</head>
stuff.html
available as a template within the parent page, but you'd have to use scripting to create clones of its DOM in the parent page so that they're visible to the user.
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.
<div data-include="/path/to/include.html"></div>
. This tool makes it easier to make a simple no-plugin multipage mockup in a clean way.
No need for scripts. No need to do any fancy stuff server-side (tho that would probably be a better option)
<iframe src="/path/to/file.html" seamless></iframe>
Since old browsers don't support seamless, you should add some css to fix it:
iframe[seamless] {
border: none;
}
Keep in mind that for browsers that don't support seamless, if you click a link in the iframe it will make the frame go to that url, not the whole window. A way to get around that is to have all links have target="_parent"
, tho the browser support is "good enough".
seamless
attribute has been removed from the HTML draft it came from. Don't use it.
A simple server side include directive to include another file found in the same folder looks like this:
<!--#include virtual="a.html" -->
Also you can try:
<!--#include file="a.html" -->
<!--#include file="a.html" -->
as well
web.config
file in the <handlers>
section
A very old solution I did met my needs back then, but here's how to do it standards-compliant code:
<!--[if IE]>
<object classid="clsid:25336920-03F9-11CF-8FD0-00AA00686F13" data="some.html">
<p>backup content</p>
</object>
<![endif]-->
<!--[if !IE]> <-->
<object type="text/html" data="some.html">
<p>backup content</p>
</object>
<!--> <![endif]-->
<object>
, <embed>
and <iframe>
all work for this, but in all three cases they create separate documents with their own styling and scripting contexts (iframe particularly includes ugly borders and scrollbars), and for instance any links by default open within them rather than on the parent page (although this can be overridden with target="_parent"). From all of these, iframe is the only one that has some hope of becoming more integrated through the HTML5 seamless
attribute (mentioned by bjb568), but it's not yet well supported: caniuse.com/#feat=iframe-seamless
seamless
attribute) is the only outdated part -- the rest still applies.
Following works if html content from some file needs to be included: For instance, the following line will include the contents of piece_to_include.html at the location where the OBJECT definition occurs.
...text before...
<OBJECT data="file_to_include.html">
Warning: file_to_include.html could not be included.
</OBJECT>
...text after...
Reference: http://www.w3.org/TR/WD-html40-970708/struct/includes.html#h-7.7.4
$(function(){})
will only execute after the document finishes loading.XMLHttpRequest cannot load file:///.../b.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.