Skip to content

Instantly share code, notes, and snippets.

@JacobLett
Created December 30, 2021 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobLett/9744552713ef1be1464095af1adf5d14 to your computer and use it in GitHub Desktop.
Save JacobLett/9744552713ef1be1464095af1adf5d14 to your computer and use it in GitHub Desktop.
Load external header and footer html with jQuery
/*
####################################################################
Include External Header & Footer -----------------------------------
# external server needs to have CORS - enabled https://enable-cors.org/
# more examples - https://www.tutorialrepublic.com/jquery-tutorial/jquery-ajax-load.php
####################################################################
*/
$(document).ready(function() {
// Give the body a class so that you can write styles to target the page
$("body").addClass('externalhf');
// Add placeholders for header and footer html
$("body").prepend('<div id="externalhfHeader"></div>');
$("body").append('<div id="externalhfFooter"></div>');
// load header html
$( "#externalhfHeader" ).load( "//www.externalsite.com/include-hf/header.html?v=1", function() {
console.log( "external header loaded" );
});
// includes/footer-nav.html
$( "#externalhfFooter" ).load( "//www.externalsite.com/include-hf/footer.html?v=1", function() {
console.log( "external footer loaded" );
});
// load external styles
$("head").append('<link rel="stylesheet" href="//www.externalsite.com/include-hf/nav.css" type="text/css"/>');
//END DOC READY
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment