Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

318
Visualizações
How can I get the previous url in javascript?

I need to get the previous url in a nextjs project. I found this but it doesn't help because it always prints me the base url (http://localhost:3000/) with this method (document.referrer). I also tried to push a state into window.history based on official developer mozilla doc.

In the last case I click on a link and it redirects me to another page. On this page I need to know where I come from. I tried:

//pathname is a string containing the actual pathname
history.pushState({prevPath: pathname},pathname)

If I print history in the new page then I don't see any prevState in the state field.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can simply create a state which is an object that has two properties: previousUrl and currentUrl like this:

const urlReducer = (state={previousUrl: null, currentURL: null},action) => {
    switch(action.type){
        case "SET_URLS":
            return {previousUrl: state.currentUrl, currentUrl: window.location.href}
        default:
            return state;
    }
}

Then all you need to do is dispatch an action in every page you visit like this:

const dispatch = useDispatch();

useEffect(()=>{
    dispatch({type: "SET_URLS"});
},[]);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda