79406800

Date: 2025-02-02 14:58:09
Score: 1.5
Natty:
Report link

Finally figured this one out, with some help from a friend. He changed the js and explained how to achieve this (thanks, SH!).

This works for my needs:

let currentPageIndex = 0;

// Get the module name (i.e., "name-of-game" from folder name like "name-of-game xxxCSS")
function getModuleName() {
  const pathParts = window.location.pathname.split('/');
  const folderName = pathParts[pathParts.length - 2]; // Get the folder name (second to last part)
  const moduleName = folderName.split(' ')[0]; // Get the first part before the space
  return moduleName;
}

// Get dynamic $moduleName (folder name)
const moduleName = getModuleName();

// Define the navigation order for pages with dynamic module name
const pages = [
  `${moduleName} index.html`,
  `${moduleName} setting.html`,
  `${moduleName} cast.html`,
  `${moduleName} rules.html`,
  `${moduleName} weblog.html`
];

window.onload = function () {
  // Get current page name from URL
  const currentPath = window.location.pathname;
  const currentPage = decodeURI(
    currentPath.substring(currentPath.lastIndexOf("/") + 1)
  );
  
  // DOM Elements for navigation buttons
  const buttonPrevious = document.getElementById("navigatePrevious");
  if (buttonPrevious) {
    buttonPrevious.onclick = navPrev;
  }

  const buttonNext = document.getElementById("navigateNext");
  if (buttonNext) {
    buttonNext.onclick = navNext;
  }

  // Helper to find the index of the current page in the navigation order
  currentPageIndex = pages.indexOf(currentPage);

  // Attach the "Up" navigation to a button (if it exists)
  const buttonUp = document.getElementById("navigateUp");
  if (buttonUp) {
    buttonUp.onclick = navUp;
  }

  // Initialize navigation buttons
  updateButtons();
};

// Update button states and navigation links
function updateButtons() {
  // Disable left nav if on the first page
  const buttonPrevious = document.getElementById("navigatePrevious");
  if (buttonPrevious) {
    buttonPrevious.disabled = currentPageIndex <= 0;
    if (currentPageIndex > 0) {
      buttonPrevious.onclick = () => {
        window.location.href = pages[currentPageIndex - 1];
      };
    }
  }

  // Disable right nav if on the last page
  const buttonNext = document.getElementById("navigateNext");
  if (buttonNext) {
    buttonNext.disabled = currentPageIndex >= pages.length - 1;
    if (currentPageIndex < pages.length - 1) {
      buttonNext.onclick = () => {
        window.location.href = pages[currentPageIndex + 1];
      };
    }
  }
}

// Function for "Up" navigation
function navUp() {
  window.location.href = `../${moduleName} weblog.html`; // Always go to the top-level Archive page
}

// Function to navigate to the previous page
function navPrev() {
  if (currentPageIndex > 0) {
    window.location.href = pages[currentPageIndex - 1];
  }
}

// Function to navigate to the next page
function navNext() {
  if (currentPageIndex < pages.length - 1) {
    window.location.href = pages[currentPageIndex + 1];
  } else {
    // Handle case when on the last page, if needed
  }
}
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Blacklisted phrase (1): how to achieve
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: K-RAM