Created
May 3, 2025 19:19
-
-
Save sreekotay/08f9dfcd7553abb8f1bb17375d601633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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