In your "header" and "footer", do you plan on having tags such as <html> <head> <body>, etc? I'm presuming you don't because things can get more complicated.
Just use a popular library to help you with simple DOM tasks. You can use the previously mentioned framework but if you really just want to insert some content, JavaScript + library is pretty simple.
Example:
http://plnkr.co/edit/nIyDyJvQDwM3TIfj0kRB?p=preview
Full source:
|
<!DOCTYPE html>
<html>
<head>
<!-- include the library here -->
<script data-require="jquery@1.11.3" data-semver="1.11.3" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<header></header>
<h1>Hello, World!</h1>
<footer></footer>
<!-- code for including header/footer -->
<script type="text/javascript">
// we're ready, include away
$(window).ready(function(){
// use easy AJAX jQuery helpers
// add header.html into <header>
$.get("header.html", function(fileData){
$('header').html(fileData);
});
// add footer.html into <footer>
$.get("footer.html", function(fileData){
$('footer').html(fileData);
});
});
</script>
</body>
</html>
|
Just paste that in a file, name it html and load it up on a server, same as the live example above.