31

How can I insert an external html file to my file?

For instance:

<div id="header">

       Here show the external HTML code in the file, for example: name.html

</div>

Thank you very much!

user2467899
  • 533
  • 2
  • 5
  • 19
  • Please check similar questions before making a new one, I'm sure this has been asked many times before. – Ozzy Jun 17 '13 at 13:17
  • If there are other questions which have better answers, could someone please link to them? Thanks – Luke Griffiths Jun 09 '14 at 18:13
  • 1
    @kapa I'm voting to re-open because (1) the linked question asks about best practices and this one asks a practical how-to question, (2) the linked question is about templates in general and this one is specifically about including the contents of an external file (which can have other uses beyond just templating), (3) The answers on that question and this question are totally different with little if any overlap. – JBentley May 04 '16 at 20:16
  • 1
    To expand on point (2), I needed to know how to include external files so that I could share content between flashcards in [Anki](https://en.wikipedia.org/wiki/Anki_%28software%29). None of the answers in the other question are relevant because they all assume the use of a browser (e.g. with JavaScript) or webserver (e.g. with PHP). This question, and the answers, can apply to uses of static HTML other than websites. – JBentley May 04 '16 at 20:17
  • duplicate of [Include another HTML file in a HTML file](https://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file) – João Pimentel Ferreira Dec 28 '20 at 21:37

4 Answers4

52

Another way is to use the object tag. This works on Chrome, IE, Firefox, Safari and Opera.

<object data="html/stuff_to_include.html"> 
    Your browser doesn’t support the object tag. 
</object>

more info at http://www.w3schools.com/tags/tag_object.asp

mmohab
  • 2,073
  • 4
  • 23
  • 40
Stuart G
  • 537
  • 5
  • 2
  • 3
    This causes issues with hrefs. They load within the frame of the object by default. You need to mark the hrefs target as parent http://stackoverflow.com/a/1037870/852806 – HockeyJ Oct 19 '16 at 15:42
  • 1
    this worked, but all the CSS for my div's in the stuff_to_include.html is being ignored. How do I get the styling back onto it? – Robbie Nov 30 '19 at 19:59
  • @Robbie Have you find any solution? – ppusapati Jul 14 '20 at 08:10
11

You can use jquery load for that.

<script type="text/javascript">
$(document).ready(function(e) {
    $('#header').load('name.html',function(){alert('loaded')});
});
</script>

Don't forget to include jquery library befor above code.

Rajnikant Kakadiya
  • 1,935
  • 1
  • 22
  • 29
  • 7
    While technically correct (this will, indeed, load an external HTML file), this is like doing kidney surgery with a backhoe. Since the question isn't even tagged jQuery, this suggests going out and getting a backhoe for the express purpose of performing kidney surgery -- even worse. – Chris Baker May 21 '14 at 17:30
7

You're looking for the <iframe> tag, or, better yet, a server-side templating language.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
1

The iframe element.

<iframe src="name.html"></iframe>

But content that you way to have appear on multiple pages is better handled using templates.

Community
  • 1
  • 1
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • But in this way I see the source code, right? Because my purpose is that: my header, menu and footer appear in many other HTML file. In order to don't repeat any time the code I prefer write one HTML for each section and then load it in the page (in this way maintenance is more easier). – user2467899 Jun 17 '13 at 13:20
  • "http://stackoverflow.com/a/16132516/19068" — Only if you view source. – Quentin Jun 17 '13 at 13:21
  • "Because my purpose is that: my header, menu and footer appear in many other HTML file." — Then use a template instead of an HTML feature. – Quentin Jun 17 '13 at 13:21