5

I'm using localStorage to back up form data on a long form. So if a user refreshes they will not lose everything. This is working perfectly on Chrome & Firebox on the computer.

However on mobile (iOS at least) It's not working at all. Any advice?

Here is a small part of my code as an example:

Storing Data:

 window.onbeforeunload = function() {
       window.localStorage.setItem("Job1", $("#topScore1B").text());
    }

Retrieving Data:

window.onload = function() {
    //   If values are not blank, restore them to the fields
        var name = window.localStorage.getItem('Job1');
        if (name !== null) $('#job1').text(name);


}
AndrewLeonardi
  • 3,351
  • 9
  • 47
  • 100
  • Does it work if you set it during normal interaction rather than in `onbeforeunload`? – Ry- Mar 07 '17 at 03:19
  • Are you using safari in incognito mode? The `setItem` will throw an exception if it is unable to write to localStorage, which is the case with incognito safari for example. – Niklas Mar 07 '17 at 03:19
  • @Ryan If you're asking does the ID work in other uses then yes. – AndrewLeonardi Mar 07 '17 at 03:20
  • @Niklas It is not working in regular Safari – AndrewLeonardi Mar 07 '17 at 03:21
  • @AndrewLeonardi: I don’t know what “the ID” is, but if you use `setItem` in a click listener or something instead of `onbeforeunload`, are you able to read it with `getItem` on mobile? – Ry- Mar 07 '17 at 03:21
  • @Ryan Hmm no I cannot seem that to work on mobile either. – AndrewLeonardi Mar 07 '17 at 03:28
  • localStorage does indeed work on Android Chrome, iPad and maybe all mobile devices that run Chrome. I've written this up with a plunkr code example you can point your device at and see it work : https://stackoverflow.com/questions/46833440/localstorage-is-null-in-chrome-mobile-android/56477550#56477550 – raddevus Jun 06 '19 at 12:28

1 Answers1

5

The beforeunload event does not fire on iOS.

It looks like unload does fire, so try storing your data into local storage there instead.

Alternatively, you could store data into local storage as the user edits the form (ie when each input fires change), which comes with the added bonus of storing the entered data even if the browser crashes before the user finishes.

Community
  • 1
  • 1
josh3736
  • 139,160
  • 33
  • 216
  • 263