MCPcopy Create free account
hub / github.com/FThompson/FormPersistence.js / persist

Function persist

form-persistence.js:34–58  ·  view source on GitHub ↗

* Registers the given form for persistence by saving its data to local or session storage. * Saved form data will be stored upon page refresh and cleared upon form submission. * Saved form data will be loaded upon calling this function, typically on page load. * * @param {HTMLFo

(form, options)

Source from the content-addressed store, hash-verified

32 * is excluded if the function returns true.
33 */
34 function persist(form, options) {
35 let defaults = {
36 saveOnSubmit: false
37 }
38 let config = Object.assign({}, defaults, options)
39 load(form, config)
40 // Some devices like ios safari do not support beforeunload events.
41 // Unload event does not work in some situations, so we use both unload/beforeunload
42 // and remove the unload event if the beforeunload event fires successfully.
43 // If problems persist, we can add listeners on the pagehide event as well.
44 let saveForm = () => save(form, config)
45 let saveFormBeforeUnload = () => {
46 window.removeEventListener('unload', saveForm)
47 saveForm()
48 }
49 window.addEventListener('beforeunload', saveFormBeforeUnload)
50 window.addEventListener('unload', saveForm)
51 if (!config.saveOnSubmit) {
52 form.addEventListener('submit', () => {
53 window.removeEventListener('beforeunload', saveFormBeforeUnload)
54 window.removeEventListener('unload', saveForm)
55 clearStorage(form, config)
56 })
57 }
58 }
59
60 /**
61 * Serializes the given form into an object, excluding password and file inputs.

Callers

nothing calls this directly

Calls 2

loadFunction · 0.85
clearStorageFunction · 0.85

Tested by

no test coverage detected