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

350
Visualizações
In SvelteKit, is there a way to cancel a popstate event if user state isn't saved?

I have a component that allows a user to edit their state. This triggers a notSaved variable. I have a beforeunload event handler to handle reload and exiting the page to remind the user to save their state, but using SvelteKit, using the back button in the browser doesn't seem to trigger the beforeunload event. I also have a popstate event handler, because that is triggered when the back button is clicked, but I can't figure out how to prevent the window.history from changing if the notSaved variable is true.

Is there a way in SvelteKit to trigger a Changes you made may not be saved. popup similar to the one that is triggered on a beforeunload event when the back or forward button is pressed?

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

0

You can use the beforeNavigate function to be notified of a navigation intent in order to potentially block it, for example:

<script>
    import { beforeNavigate } from '$app/navigation';

    let unsavedWork = false;
    let value = 0;

    beforeNavigate(({ from, to, cancel }) => {
        if (unsaved) {
            cancel();
            showUnsavedWorkPopup();
        }
    });

    function onChange() {
        ...
        unsavedWork = true;
    }

    async function save() {
        await ...
        unsavedWork = false;
    }
</script>

<input type="number" bind:value on:change={onChange}>
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