I want home.html to load in
This approach makes use of modern Javascript features like The See the fetch API documentation for more details.
Fetching HTML the modern Javascript way
async/await and the fetch API. It downloads HTML as text and then feeds it to the innerHTML of your container element./**
* @param {String} url - address for the HTML to fetch
* @return {String} the resulting HTML string fragment
*/
async function fetchHtmlAsText(url) {
return await (await fetch(url)).text();
}
// this is your `load_home() function`
async function loadHome() {
const contentDiv = document.getElementById("content");
contentDiv.innerHTML = await fetchHtmlAsText("home.html");
}
await (await fetch(url)).text() may seem a bit tricky, but it's easy to explain. It has two asynchronous steps and you could rewrite that function like this:async function fetchHtmlAsText(url) {
const response = await fetch(url);
return await response.text();
}