function fallbackHTML(html) {
// Check if the fallback HTML is defined.
if (typeof fallbackHTML === 'undefined') {
return null; // No fallback defined.
}
// Return the fallback HTML.
return fallbackHTML;
}
// Example fallback HTML (can be defined globally or passed as an argument)
const fallbackHTML = `
<!DOCTYPE html>
<html>
<head>
<title>Fallback</title>
<style>
body { font-family: sans-serif; }
</style>
</head>
<body>
<h1>Something went wrong.</h1>
<p>Please try again later.</p>
</body>
</html>
`;
// Example usage (replace with your actual fallback logic)
function loadPage(url) {
fetch(url)
.then(response => {
if (!response.ok) {
// Fallback if the main page fails to load.
const fallback = fallbackHTML();
if (fallback) {
document.documentElement.innerHTML = fallback; // Replace the content of the page
} else {
document.documentElement.innerHTML = "<h1>Error loading page</h1>";
}
}
return response.text();
})
.then(text => {
document.documentElement.innerHTML = text; // Load the main page content
})
.catch(error => {
// Fallback if there's a network error.
const fallback = fallbackHTML();
if (fallback) {
document.documentElement.innerHTML = fallback; // Replace the content of the page
} else {
document.documentElement.innerHTML = "<h1>Error loading page</h1>";
}
});
}
Add your comment