Vous remarquerez que les solutions de double soumission suivantes
sont toutes assez différentes.
Je n’essaye pas d’ajouter de cohérence car elles ont toutes leurs
subtilités qui m’ont permis d’apprendre des propriétés de JavaScript.
;(function(){functionstopClickEvent(ev){ev.preventDefault()ev.stopPropagation()}document.body.addEventListener('click',function(ev){if(ev.target.tagName==='A'||ev.target.getAttribute('type').toLowerCase()==='submit'){setTimeout(function(){// Needs to happen _after_ the request goes through, hence the timeoutev.target.addEventListener('click',stopClickEvent)},0)setTimeout(function(){ev.target.removeEventListener('click',stopClickEvent)},500)// (1)}})})()
functiondisableMultipleSubmits(){// by Andrea Giammarchi - WTFPLArray.prototype.forEach.call(document.querySelectorAll('form[disablemultiplesubmits]'),function(form){form.addEventListener('submit',this,true)},{// button to disablequery:'input[type=submit],button[type=submit]',// delay before re-enablingdelay:500,// handlerhandleEvent:function(e){varbutton=e.currentTarget.querySelector(this.query)button.disabled=truesetTimeout(function(){button.disabled=false},this.delay)},})}
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication// http://creativecommons.org/publicdomain/zero/1.0/;(function(win,doc){// Cut the mustard. (1)if(!win.localStorage)return// You should probably use a more specific selector than this.vartextarea=doc.querySelector('textarea')// The key for the key/value pair in localStorage is the current URL.varkey=win.location.hrefvaritem=null// Use the 'pagehide' event in modern browsers or 'beforeunload' in older browsers.varunloadEventif('onpagehide'inwin){unloadEvent='pagehide'}else{unloadEvent='beforeunload'}// If there's a localStorage entry for the current URL, update the textarea with the saved value.item=win.localStorage.getItem(key)if(item){vardata=JSON.parse(item)textarea.value=data.content}// This function will store the current value of the textarea in localStorage (or delete it if the textarea is blank).functionupdateStorage(){if(textarea.value){item=JSON.stringify({content:textarea.value})win.localStorage.setItem(key,item)}else{win.localStorage.removeItem(key)}// This event listener is no longer needed now so remove it.win.removeEventListener(unloadEvent,updateStorage)}// When the user presses a key just *once* inside the textarea, run the storage function when the page is unloaded.textarea.addEventListener('keyup',function(){win.addEventListener(unloadEvent,updateStorage)win.setInterval(updateStorage,60000)},{once:true})// When the form is submitted, delete the localStorage key/value pair.textarea.form.addEventListener('submit',function(){win.localStorage.removeItem(key)win.removeEventListener(unloadEvent,updateStorage)})})(this,this.document)
Comme le fait remarquer Jeremy en commentaire, il faut adapter le
sélecteur car vous avez probablement plus d’un textarea dans la page.
Il serait possible de le rendre générique mais il y a peut-être des
cas où vous ne voulez pas que ça sauvegarde, je préfère le laisser
en explicitement activable.