0

I am trying to find a way to change my html page on button press seamlessly (imagine pageA.html ends with a fullscreen image -> pageB.html starts with the same fullscreen image).

I have tried manually modifying the DOM using AJAX, but the issue is the new body is loaded before the new head, so the new JS files are still undefined thus the page breaks:

function loadNextPage() {
    $.ajax({
        url: "next.html",
        method: 'GET',
        dataType: 'html',
        success: function (data) {
            let parser = new DOMParser();
            let dom_document = parser.parseFromString(data, "text/html");
            let head_element = dom_document.getElementsByTagName("head")[0];

            $('head').html(head_element.innerHTML);
            $('body').html(body_element.innerHTML);
        }
    });
}

I have also tried using Barba.js but (a) I could not get it working, and (b) as I understand it is best for actual page transition, something that I do not need. (I also tried infiniteajaxscroll but I could not preload the next page).

Note: ideally I want to preload and cache the next page until it is needed to switch.

Is there another library or way for me to do this?

2
  • You could render both pages, just hide the one you dont want to display initially. e.g. position it off screen, display none, visibility hidden, etc. Commented Jan 25, 2024 at 2:14
  • Depending on your browser support requirements, you may want to try view transitions Commented Jan 25, 2024 at 2:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.