How to redirect to another page with the Back button

Asked

Viewed 561 times

0

I need to "replace" the function of the browser back button, so that when the user clicks it is redirected to a page. I’m already using a snippet I found but its operation is not very consistent, and if the user has not clicked at least once on the screen, it does not work.

<script>    
    (function(window, location) {
    history.replaceState(null, document.title, location.pathname+"#!/history");
    history.pushState(null, document.title, "");

    window.addEventListener("popstate", function() {            
        if(location.hash === "#!/history") {            
            setTimeout(function(){
            document.getElementById('breadCurso').submit();
            },0);
        }
    }, false);
    }(window, location));
</script>

I am redirecting through a form ('breadCurso' ID), so that the links are protected by the POST, and "including" the pages through PHP.

  • Encapsulate the function correctly and try again (Function() { ... })(window, Location)

  • @ruansenadev I keep having the same problem of having to click on the screen at least once before. If I don’t click, I’m redirected to the main page.

  • This is not very interesting, tends to make the user angry of the site, there is no way to prevent him from using the button, just as there is no way to block the console, more you can change the history, so if he press back, would go to another page, more is not good, mainly.

1 answer

1


You can use localstorage to store a variable on the page that Voce does not want the user to return, if it goes back and the variable is set you redirect to another page.

if(!localStorage.getItem("verifica_se_passou_nesta_pagina")){
localStorage.setItem("verifica_se_passou_nesta_pagina","passou")
} else{
window.history.forward(); // envia para á pagina que o usuário estava
}

localstorage will only be deleted if the user clears the history and cache.

To prevent page refresh to resend a form, one should check if the request is a POST, the following example I removed from the link:

<?php
session_start();

if( $_SERVER['REQUEST_METHOD']=='POST' )
{
    $request = md5( implode( $_POST ) );
    
    if( isset( $_SESSION['last_request'] ) && $_SESSION['last_request']== $request )
    {
        echo 'refresh';
    }
    else
    {
        $_SESSION['last_request']  = $request;
        echo 'post';
    }
}

?>

<form action="" method="post">
        <input type="text" name="ae" value="ae" />
        <input type="submit" name="enviar" value="enviar" />
    </form>

  • Thank you for the answer, it is functional but not applicable in my problem. My bigger question is that whenever I update or use the back button, it asks to resend the form, since I am redirecting the pages through POST forms, so I am trying to "replace" the back function by a redirect function, before the next page loads.

  • Now I understand your question, I edited my answer!

  • If the issue is the resubmission of the POST Form, you can use this Pattern https://en.wikipedia.org/wiki/Post/Redirect/Get. If you are not looking to work with the state object available in popstate history and Event

Browser other questions tagged

You are not signed in. Login or sign up in order to post.