Notes

How to include files in HTML

Edit on GitHub

Web Development
2 minutes

via JavaScript

a.html

 1<html>
 2  <body>
 3    <h1>Put here your HTML content before insertion of b.js.</h1>
 4    ...
 5
 6    <script src="b.js"></script>
 7
 8    ...
 9
10    <p>And here whatever content you want afterwards.</p>
11  </body>
12</html>

b.js:

1document.write('\
2\
3    <h1>Add your HTML code here</h1>\
4\
5        <p>Notice however, that you have to escape LF's with a '\', just like\
6        demonstrated in this code listing.\
7    </p>\
8\
9');

JavaScript is preferred since jQuery is that jQuery.js is ~90kb in size. Keep the amount of data to load as small as possible.

In order to insert the escape characters without much work, it is recommended to use a simple regular expression that matches whole lines (^.*$) and adds \ at the end of each line. For example, you could use sed on the command line like this:

1sed 's/^.*$/&\\/g;' b.html > escapedB.html

via jQuery

a.html:

 1<html>
 2  <head>
 3    <script src="jquery.js"></script>
 4    <script>
 5      $(function () {
 6        $('#includedContent').load('b.html')
 7      })
 8    </script>
 9  </head>
10
11  <body>
12    <div id="includedContent"></div>
13  </body>
14</html>

b.html:

```html

This is my include file

```

jQuery .load() Documentation

via Server Side Includes (SSI)

A simple server side include directive to include another file found in the same folder looks like this:

1<!-- #include virtual="a.html" -->

configuring SSI for your server