Automate AJAX Includes

Purpose: This exercise is a continuation of Exercise 1.2, . Do Exercise 1.2 first.

This exercise assumes the sample webpage from Exercise 1.2 is still in your document root with the modifications you made in that exercise.

Use HTML data-* attributes

Before HTML5 in 2008, there was no simple way to put custom attributes in HTML tags. Only the pre-defined attributes were recognized, and Javascript would not see attributes other than those. The HTML5 standard allowed custom attributes so long as they begin with the prefix "data-" and are all lower-case. Practice using data-* attributes to automate your includes, then add a footer to include, as follows:

Automate includes

Add the following to index.js:

Now, in to index.html, about.html, and contact.html:

Replace this script:

with this one:

<script>includeall();</script>

and change the nav element as follows:

<nav data-include="nav.html"></nav>

It should work as before,

Add a Footer

Create footer.html:

Now, add to index.html, about.html, and contact.html, on the bottom:

<footer data-include="footer.html"></footer>

If done, right, this footer should now be included automatically when you load your page. Now you can add includes easily.

Don't Delete Your Code Yet

The next exercise uses it.