Skip to content

Instantly share code, notes, and snippets.

@sreekotay
Created May 3, 2025 19:19
Show Gist options
  • Save sreekotay/08f9dfcd7553abb8f1bb17375d601633 to your computer and use it in GitHub Desktop.
Save sreekotay/08f9dfcd7553abb8f1bb17375d601633 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Importer Demo</title>
<script>
/**
* Synchronously fetches the given URL and writes its contents
* into the document at this script’s position.
*
* @param {string} url Path to the .html fragment you want to include
*/
function includeHTML(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false); // false = synchronous
xhr.send(null);
if (xhr.status === 200 || xhr.status === 0) {
document.write(xhr.responseText);
} else {
document.write('<!-- includeHTML failed: ' + xhr.status + ' -->');
}
}
</script>
</head>
<body>
<h1>Main Page</h1>
<!-- This will pull in fragment.html synchronously -->
<script>includeHTML('frag.html');</script>
<p>Rest of the page...</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment