Forhindrer anchor links i at blive tilføjet til URL’en

Denne funktion forhindrer anchor-links i at tilføje #fragmenter til URL’en, så adressen forbliver uændret ved klik, og siden stadig scroller korrekt.

Tilføj til Elementor Brugerdefineret kode - body Start:

<script>
jQuery(document).ready(function ($) {
  function auxoGetAnchorTarget(targetHash) {
    if (!targetHash || targetHash === "#") return null;

    var safeId = targetHash.replace("#", "");
    safeId = (window.CSS && typeof CSS.escape === "function")
      ? CSS.escape(safeId)
      : safeId.replace(/([ #;?%&,.+*~\':"!^$[\]()=>|\/@])/g, "\\$1");

    return document.getElementById(safeId) || document.querySelector('[name="' + safeId + '"]');
  }

  function auxoScrollToTarget(targetElement) {
    var useSmoothScroll = true; // sæt til false hvis du ikke vil have smooth scroll

    targetElement.scrollIntoView({
      behavior: useSmoothScroll ? "smooth" : "auto",
      block: "start"
    });
  }

  $(document).on("click", 'a[href^="#"]', function (event) {
    var targetHash = $(this).attr("href");
    var targetElement = auxoGetAnchorTarget(targetHash);

    // Hvis target ikke findes, lad browseren håndtere det normalt
    if (!targetElement) return;

    // Stop browseren i at skrive hash i URL (så der ikke kommer "split-sekund" hash)
    event.preventDefault();

    // Hold URL ren
    if (window.history && typeof window.history.replaceState === "function") {
      window.history.replaceState(null, document.title, window.location.pathname + window.location.search);
    }

    // Scroll til target
    requestAnimationFrame(function () {
      auxoScrollToTarget(targetElement);
    });
  });
});
</script>