0

I am trying to put the Back Button Redirect Script function to good use. I have a plugin which plays background (user-initiated) music on my site. As long as the user clicks forward, the music streams continuously and nearly uninterrupted from page to page, without restarting. If the user clicks the back button (or refreshes), the music stops and they must manually press play to restart the stream. The author says they have no way to resolve it. I'm not giving up just yet.

My thought is, why not use JavaScript to record the browser's previous page URL, then capture the back button trigger and send the user "forward" to that URL, thus keeping the music stream intact while honoring the user's desire to go back a page?

Conceptually, being a supernoob at JavaScript, I patched this together from different sources on here and codingbeautydev...

$(window).bind("onpopstate", function (e) {
    const previousPage = document.getElementById("previous-page");
    previousPage.textContent = document.referrer;

    window.history.pushState({ page: 1 }, "", "");
    window.onpopstate = function (event) {
        if (event) {
            window.location.href = previousPage;
        }
    };
});

My first thought is there are surely some syntex errors in there at my doing and potentially much more that need be modified, but I'm hoping someone can easily touch up my rough sketch. Additionally, beyond making this work, I see the limits of this allowing only 1-page of history, and I'm curious if there's a way to nest it into a stack of a few pages to which could be visited in reverse order, all the while moving "forward". First things first though, then on to bigger and better.

Thanks guys! Mark

SimPub
  • 3
  • 3
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Menelaos Vergis Dec 14 '22 at 09:31
  • I just want to send someone (via location.href) to the previous page they were on when they press the back button. I don't want the back button to function the default way. I need it to actually act like a link moving forward, not backwards. i.e. I am on www.site.com/paper, then I click a link on the page to www.site.com/boxes and that loads. Next I hit the back button, but instead of letting the normal back function work, I want to instead find out what that previous page URL was (www.site.com/paper) and use location.href to load that page instead. Hope that helps! Thanks! – SimPub Dec 14 '22 at 10:26
  • Your comment is more straightforward than the actual question. You have to follow the StackOverflow question guidelines to form a proper question. I suggest you update the question to only one question. Clear the text to not contain any personal greetings or humor (thanks guys, Mark. I am not giving up yet, Let's start here and see how it goes... etc). Change the title to 'Override the default action of browser's back button', and ask only about the specific issue. Good job on adding code. – Menelaos Vergis Dec 14 '22 at 12:54

1 Answers1

0

You cannot change the default behavior of the browsers's back or forward button unless your app uses URL hashes to navigate, but from my understanding of your question the user actually goes from say .com/paper to .com/boxes and not .com/paper#page1 to .com/paper#page2.

One possible option you could try is using the following (from here):

window.addEventListener('pageshow', function (event) {
    if (event.persisted || performance.getEntriesByType("navigation")[0].type === 'back_forward') {
        // User got here from using the Back or Forward button
    }
});

This will trigger when the user got on the page this code runs on using the back or forward window button, also if the user goes from /boxes back to /paper.

You can try to save the current state of the music playing on the background (which song, timestamp, audio level, etc) in local storage (at max) every second or so, and get the stored values inside the function above to continue the music the user was last listening to when he left the previous page. Not the most elegant solution, but all I think of right now that might actually work.

Edit:

The code you requested. Chrome & Safari will block/ignore it due to history manipulation, except when an user interacts with the page first. It's not how history should be used. Don't use it in production, but play with it all you want. Also, here's an simple example how history can be used.

window.history.pushState({}, '', window.location.pathname);
let previousPage = document.referrer;
window.addEventListener('popstate', (e) => {    
    window.location.assign(previousPage)
});  
Cooleronie
  • 1,225
  • 1
  • 9
  • 9
  • I am not sure I understand your proposal, but as far as the music goes, there is nothing I need to do to perpetuate the playback other than to not navigate backwards. Clicking any link on the site continuously streams the music in the background. Is there a way on any given webpage to call for the URL of the page from which the user came? i.e. lastPage = theURL.iwasat.wheniclicked.thelink.togethere If that be the case, I see people redirect users when they click the back button to any site they want. I just want them to hit the back button and be FORWARDED to lastPage. – SimPub Dec 15 '22 at 04:18
  • What you want will 1) only work in SPAs and 2) cause a loop between the current page and the first page in the history stack. This is history manipulation and both Chrome & Safari have a protection in place for exactly that, to avoid users getting trapped. So it won't work. Somehow Firefox allows it, though. I added some code in my answer that does what you want, however it only works in Firefox (in Chrome for example you have to interact with the page first for `popstate` to fire). – Cooleronie Dec 15 '22 at 21:21
  • Okay, I understand. I do appreciate you adding this. It's still helpful for me to understand and practice. I see what you are saying with the loop. I was concerned about that myself, and I was thinking that the next step would be to implement a manner of building a stack from the first landing page and managing it manually so the user could navigate backwards, if not all the way, then perhaps maybe 5 or 10 pages to make it practical. In the end, I always want to make the user happy. At this stage, it seems the tradeoff is not quite the precise answer to handling the music the best way. – SimPub Dec 18 '22 at 09:46