|
1 | | - |
2 | | - // Get all the navbar buttons |
3 | | - const navbarLinks = document.querySelectorAll('.navbar-link'); |
4 | | - |
5 | | - // Reference to dynamic content loader |
6 | | - const dynamicContentLoader = document.getElementById('dynamicContentLoader'); |
7 | | - |
8 | | - // Function to remove 'active' class from all navbar links |
9 | | - function deactivateAllLinks() { |
10 | | - navbarLinks.forEach(link => link.classList.remove('active')); |
11 | | - } |
12 | | - |
13 | | - // Function to load content based on the button clicked |
14 | | - function loadContent(page) { |
15 | | - // Create the article dynamically based on the page name |
16 | | - const article = document.createElement('article'); |
17 | | - article.classList.add(page); // Add the page name as a class |
18 | | - article.classList.add('active'); // Make it active |
19 | | - |
20 | | - // Create header and title for the article |
21 | | - const header = document.createElement('header'); |
22 | | - const h2 = document.createElement('h2'); |
23 | | - h2.classList.add('h2', 'article-title'); |
24 | | - h2.textContent = `${page.charAt(0).toUpperCase() + page.slice(1)} Page`; // Capitalize the first letter of page |
25 | | - header.appendChild(h2); |
26 | | - |
27 | | - // Create the content section |
28 | | - const section = document.createElement('section'); |
29 | | - section.classList.add('content'); |
30 | | - section.innerHTML = `<p>This is the ${page} content. You can replace this with dynamic content or fetch data from a URL.</p>`; |
31 | | - |
32 | | - // Append content to the article |
33 | | - article.appendChild(header); |
34 | | - article.appendChild(section); |
35 | | - |
36 | | - // Clear existing content and append the new article |
37 | | - dynamicContentLoader.innerHTML = ''; // Clear previous content |
38 | | - dynamicContentLoader.appendChild(article); |
39 | | - } |
40 | | - |
41 | | - // Handle click on navbar items |
42 | | - navbarLinks.forEach(link => { |
43 | | - link.addEventListener('click', function() { |
44 | | - const page = this.getAttribute('data-nav-link'); // Get the page name from the button's data attribute |
45 | | - |
46 | | - // Deactivate all links and activate the clicked one |
47 | | - deactivateAllLinks(); |
48 | | - this.classList.add('active'); |
49 | | - |
50 | | - // Load content dynamically for the clicked page |
51 | | - loadContent(page); |
| 1 | +// Get the query parameters from the URL |
| 2 | + const params = new URLSearchParams(window.location.search); |
| 3 | + const page = params.get('page'); // Extract the 'page' parameter |
| 4 | + // Select all articles |
| 5 | + const articles = document.querySelectorAll('article'); |
| 6 | + // Default section if no valid page is provided |
| 7 | + const defaultPage = 'about'; |
| 8 | + // Flag to check if a matching page was found |
| 9 | + let isPageFound = false; |
| 10 | + // Loop through articles to show the matched one |
| 11 | + articles.forEach(article => { |
| 12 | + if (article.dataset.page === page) { |
| 13 | + article.classList.add('active'); // Add 'active' class to show |
| 14 | + isPageFound = true; // A matching page was found |
| 15 | + } else { |
| 16 | + article.classList.remove('active'); // Hide other sections |
| 17 | + } |
52 | 18 | }); |
53 | | - }); |
54 | | - |
55 | | - // Optionally load content for the first item by default |
56 | | - loadContent('about'); // Initially load the 'about' page content |
| 19 | + // Show the default page if no matching page was found |
| 20 | + if (!isPageFound) { |
| 21 | + const defaultArticle = document.querySelector(article[data-page="${defaultPage}"]); |
| 22 | + if (defaultArticle) { |
| 23 | + defaultArticle.classList.add('active'); |
| 24 | + } |
| 25 | + } |
0 commit comments