Back to last page

Asked

Viewed 2,301 times

3

I have on page (B) the following code to return to the last page (A):

<div class="return_back" onclick="window.history.go(-1);">< voltar </div>

I have a form on page (B), and if I submit it one or more times, it does not return to the last page, but stays the same.

From what I understand, windows.history something like this [A,B,B].

Is it possible to solve this? There is possibility of having access to the content of windows.history?

  • The problem is that the previous page has get variables in the link and are needed to load the required Infos

  • But the submission sends the form to where ? What the action of <form> ?

  • The form action is the current page

  • 1

    If I understood correctly, a solution would be to use ajax not to reload the page, so when you used your back button, you would go back to page A because you would not need to reload page B every time.

1 answer

1

Javascript-enabled

<script>
//Retorna o URI da página que continha o link para a página atual.
var referencia = document.referrer;

//retorna a URL da página atual
var pagina = location.href;

//pegar a parte após o ? (ponto de interrogação
var partes = pagina.toString();
var divide = partes.split("?");
var parametros = divide[1];

//contrução da url da pagina anterior com parametros
var urlVoltar = referencia+"?"+parametros;

//verifica se a sessão está ativa
if ( ! sessionStorage.getItem('voltar')){
    //O objeto Session storage permite aos usuários armazenarem os dados para uma única sessão.
    //A sessão da página dura enquanto o browser está aberto e se mantém no recarregamento da página.
    sessionStorage.setItem("voltar", urlVoltar);
}

var url= sessionStorage.getItem("voltar");

document.write('<div class="return_back" onclick="location.href=\'' + url + '\'">voltar</div>');

</script>

sessionStorage



With PHP

With PHP you can solve this using $_SESSION and $_SERVER['HTTP_REFERER']

<?php
    session_start();

    if ($_SESSION["voltar"]==""){
        $_SESSION["voltar"]=$_SERVER['HTTP_REFERER'];
    }

?>

HTML

<div class="return_back" onclick="location.href='<?php echo $_SESSION["voltar"]; ?>'">voltar </div>

$_SERVER['HTTP_REFERER address of the page through which the current page was accessed

$_SESSION are sessions that allow saving data ($variables) during the customer’s visit

Browser other questions tagged

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