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?
This is what helped me. For adding a block of html code from
b.html
toa.html
, this should go into thehead
tag ofa.html
:Then in the body tag, a container is made with an unique id and a javascript block to load the
b.html
into the container, as follows:Most of the solutions works but they have issue with jquery:
The issue is following code
$(document).ready(function () { alert($("#includedContent").text()); }
alerts nothing instead of alerting included content.I write the below code, in my solution you can access to included content in
$(document).ready
function:(The key is loading included content synchronously).
index.htm:
include.inc:
jquery include plugin on github
If you use some framework like django/bootle, they often ship some template engine. Let's say you use bottle, and the default template engine is SimpleTemplate Engine. And below is the pure html file
You can include the footer.tpl in you main file, like:
Besides that, you can also pass parameter to your dashborard.tpl.
Well, if all you're wanting to do is put text from a separate file into your page (tags in the text should work, too), you can do this (your text styles on the main page—
test.html
—should still work):test.html
sample.js
You can always recreate the HTML tags you want yourself, after all. There's need for server-side scripting just to grab text from another file, unless you want to do something more.
Anyway, what I'm starting to use this for is to make it so if I update a description common among lots of HTML files, I only need to update one file to do it (the
.js
file) instead of every single HTML file that contains the text.So, in summary, instead of importing an
.html
file, a simpler solution is to import a.js
file with the content of the.html
file in a variable (and write the contents to the screen where you call the script).Thanks for the question.
I know this is a very old post, so some methods were not available back then. But here is my very simple take on it (based on Lolo's answer).
It relies on the HTML5 data-* attributes and therefore is very generic in that is uses jQuery's for-each function to get every .class matching "load-html" and uses its respective 'data-source' attribute to load the content:
As an alternative, if you have access to the .htaccess file on your server, you can add a simple directive that will allow php to be interpreted on files ending in .html extension.
Now you can use a simple php script to include other files such as: