how to refresh page without losing form data javascript?

Today, We want to share with you refresh page without losing form data javascript.In this post we will show you keep form values after submit javascript, hear for how to refresh a div using javascript without refreshing page? we will give you demo and example for implement.In this post, we will learn about Jquery Reload Page Automatically with an example.

Free Live Chat for Any Issue

How to reload current page without losing any form data?

Example 1:

<script>
    // Run on page load
    window.onload = function() {

        // If sessionStorage is storing default values (ex. member_nm), exit the function and do not restore data
        if (sessionStorage.getItem('member_nm') == "member_nm") {
            return;
        }

        // If values are not blank, restore them to the fields
        var member_nm = sessionStorage.getItem('member_nm');
        if (member_nm !== null) $('#inputMemberNm').val(member_nm);

        var email = sessionStorage.getItem('email');
        if (email !== null) $('#inputEmail').val(email);

        var sluguen= sessionStorage.getItem('sluguen');
        if (sluguen!== null) $('#inputSluguen').val(sluguen);

        var comments= sessionStorage.getItem('comments');
        if (comments!== null) $('#inputComments').val(comments);
    }

    // Before refreshing the page, save the form data to sessionStorage
    window.onbeforeunload = function() {
        sessionStorage.setItem("member_nm", $('#inputMemberNm').val());
        sessionStorage.setItem("email", $('#inputEmail').val());
        sessionStorage.setItem("sluguen", $('#inputSluguen').val());
        sessionStorage.setItem("comments", $('#inputComments').val());
    }
</script>

How To Stop Form Resubmission On Page Refresh?

Unset Form Data

 $data = // jquery Ajax processing codes here
 unset $data;
}

Javascript

<script>
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>

Download

I hope you get an idea about javascript retain value after refresh.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.