Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

339
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!