How to include html text from another file into an HTML5 page
January 16, 2013 Leave a comment
I have heard about objects, embeds, and even iframes for this purpose. In these options I had formatting issues with the main page style not being applied. Thus, I preferred jQuery and the <article> tag.
So to keep it simple, assign an ID to your article tag,
<article id=”mycustomhtml”>
</article>
and load the page using jquery for the article mycustomhtml
<script>
$(“#mycustomhtml“).load(“customhtml.txt”, function (response, status, xhr) {
if (status == “error”) {
var msg = “Error: “;
alert(msg + xhr.status + ” “ + xhr.statusText);
}
});
< /script>
This is as simple as it gets.
I would like to thank Shakeel Abdul Rehman for his help in this snippet.