Skip to content

Commit 9a8200d

Browse files
committed
The working project version.1
1 parent ddd7e87 commit 9a8200d

File tree

2 files changed

+61
-58
lines changed

2 files changed

+61
-58
lines changed

src/App.js

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,64 @@
11
// Define the routes and their corresponding HTML files
22
const routes = {
3-
'/home': 'components/home.html',
4-
'/about': 'components/about.html',
5-
'/contact': 'components/contact.html'
6-
};
7-
8-
// Function to navigate to a specific route
9-
function navigateTo(route) {
10-
// Check if the route exists in the routes object
11-
if (routes.hasOwnProperty(route)) {
12-
// Fetch and render the HTML content of the corresponding file
13-
fetchHtml(routes[route]);
14-
} else {
15-
// Route not found, display a 404 error message
16-
renderComponent('404: Page not found.');
17-
}
3+
'/home': '/src/components/home.html',
4+
'/about': '/src/components/about.html',
5+
'/contact': '/src/components/contact.html'
6+
};
7+
8+
// Function to navigate to a specific route
9+
function navigateTo(route) {
10+
// Check if the route exists in the routes object
11+
if (routes.hasOwnProperty(route)) {
12+
// Fetch and render the HTML content of the corresponding file
13+
fetchHtml(routes[route]);
14+
} else {
15+
// the custom 404 page
16+
fetchHtml('/src/components/404.html');
17+
18+
// Route not found, display a 404 error message
19+
//renderComponent('404: Page not found.');
1820
}
19-
20-
// Function to fetch HTML content from a file
21-
function fetchHtml(path) {
22-
fetch(path)
23-
.then(response => {
24-
if (!response.ok) {
25-
throw new Error('Failed to fetch HTML file');
26-
}
27-
return response.text();
28-
})
29-
.then(html => renderComponent(html))
30-
.catch(error => {
31-
console.error(error);
32-
renderComponent('Failed to load page.');
33-
});
21+
}
22+
23+
// Function to fetch HTML content from a file
24+
function fetchHtml(path) {
25+
fetch(path)
26+
.then(response => {
27+
if (!response.ok) {
28+
throw new Error('Failed to fetch HTML file');
29+
}
30+
return response.text();
31+
})
32+
.then(html => renderComponent(html))
33+
.catch(error => {
34+
console.error(error);
35+
renderComponent('Failed to load page.');
36+
});
37+
}
38+
39+
// Function to render component content into the main content area
40+
function renderComponent(content) {
41+
document.getElementById('main-content-cive').innerHTML = content;
42+
}
43+
44+
// Function to handle changes in the URL
45+
window.onpopstate = function(event) {
46+
const route = window.location.pathname;
47+
navigateTo(route || '/home'); // Default to '/home' if no route is provided
48+
};
49+
50+
// Initial page load, check the current route from the URL
51+
window.onload = function() {
52+
const route = window.location.pathname;
53+
navigateTo(route || '/home'); // Default to '/home' if no route is provided
54+
};
55+
56+
// Function to handle navigation when a link is clicked
57+
document.addEventListener('click', function(event) {
58+
if (event.target.tagName === 'A' && event.target.getAttribute('href').startsWith('/')) {
59+
event.preventDefault(); // Prevent default navigation behavior
60+
const route = event.target.getAttribute('href');
61+
navigateTo(route);
62+
history.pushState(null, '', route); // Update browser history without causing a page reload
3463
}
35-
36-
// Function to render component content into the main content area
37-
function renderComponent(content) {
38-
document.getElementById('main-content').innerHTML = content;
39-
}
40-
41-
// Function to handle changes in the URL
42-
window.onpopstate = function(event) {
43-
const route = window.location.pathname;
44-
navigateTo(route || '/home'); // Default to '/home' if no route is provided
45-
};
46-
47-
// Initial page load, check the current route from the URL
48-
window.onload = function() {
49-
const route = window.location.pathname;
50-
navigateTo(route || '/home'); // Default to '/home' if no route is provided
51-
};
52-
53-
// Function to handle navigation when a link is clicked
54-
document.addEventListener('click', function(event) {
55-
if (event.target.tagName === 'A' && event.target.getAttribute('href').startsWith('/')) {
56-
event.preventDefault(); // Prevent default navigation behavior
57-
const route = event.target.getAttribute('href');
58-
navigateTo(route);
59-
history.pushState(null, '', route); // Update browser history without causing a page reload
60-
}
61-
});
62-
64+
});

src/components/404.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p> The page you are looking for is not found </p>

0 commit comments

Comments
 (0)