0

I am trying to set form data in local storage after submitting the form. If the page is refreshed I have to print in the console that "form submitted already". I don't know how to write reload condition My code is below

submit(){
localStorage.setItem('form-data', JSON.stringify(this.shippingForm.value));
}
window.reload(){
    let formValue = JSON.parse(localStorage.getItem('form-data'));
    console.log("form submitted alreay");
}
Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24
swetha
  • 47
  • 1
  • 8

1 Answers1

0

You just need to do use getItem. It will return null if the item doesn't exist

if (localStorage.getItem("form-data") === null) {
  // do nothing
} else {
    console.log("form submitted alreay");
}

Here is the source your can read more about it.

In your case the you can have something like this

window.reload(){
if (localStorage.getItem("form-data") === null) {
      // do nothing
    } else {
        // do your other conversions here
        console.log("form submitted alreay");
    }
}

Note : Do not forget to clear the localstorage whenever you're done

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63