Skip to content

Instantly share code, notes, and snippets.

@bitdivine
Created August 4, 2016 17:07
Show Gist options
  • Save bitdivine/4f429eaec07db631f0e192d68b9ce86e to your computer and use it in GitHub Desktop.
Save bitdivine/4f429eaec07db631f0e192d68b9ce86e to your computer and use it in GitHub Desktop.
Save html inputs in the location hash.
function get_input_dict(selector){return Array.from(document.querySelectorAll(selector || 'input')).reduce((d,n)=>{d[n.name]=n.value;return d;},{});}
function get_input_hash(selector){return Array.from(document.querySelectorAll(selector || 'input')).map((n)=>encodeURIComponent(n.name)+'='+encodeURIComponent(n.value)).join('&');}
function save_input_hash(selector){window.location.hash = get_input_hash(selector);}
function restore_input_hash(selector){window.location.hash.slice(1).split('&').map((s)=>s.match(/^([^=]*)=(.*)/)).filter((s)=>s).forEach((m)=>Array.from(document.querySelectorAll(`${selector||"input"}[name=${decodeURIComponent(m[1])}]`)).forEach((n) => n.value=decodeURIComponent(m[2])));}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment