Skip to content

Instantly share code, notes, and snippets.

@crftr
Created July 9, 2024 12:52
Show Gist options
  • Save crftr/996aef47450840d622b1edeca8e64611 to your computer and use it in GitHub Desktop.
Save crftr/996aef47450840d622b1edeca8e64611 to your computer and use it in GitHub Desktop.
Pull in and display extra content on a webpage without having to reload the whole page. It's handy for keeping things updated and flexible by loading bits of HTML from other places on the fly.
<div id="landingpageContent"></div>
<script>
fetch("{{htmlFragmentUrl}}")
.then(response => response.text())
.then(html => {
const range = document.createRange();
range.selectNode(document.getElementById('landingpageContent'));
const documentFragment = range.createContextualFragment(html);
document.getElementById('landingpageContent').appendChild(documentFragment);
})
.catch(error => {
console.error('Error fetching HTML fragment:', error);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment