javascript - Randomly generated localstorage keys -


i creating require many localstorage keys. therefore hoping use math.floor randomly generate user key.

i have used following save local storage key

var randomnumber = math.floor((math.random()*100000000)+1) var randomnumberstringify = string(randomnumber)       function savebox(id) {         var answer = document.getelementbyid('box').value;         document.write(randomnumberstringify);         localstorage.setitem(randomnumberstringify, answer);     } 

and following load key

    function loadbox() {         if (localstorage.getitem(randomnumberstringify)) {         var answer = localstorage.getitem(randomnumberstringify);     }      else {         var answer = ' ';     }      document.getelementbyid('box').value = answer;     } 

however can see load function uses "randomnumberstringify". random number

so saving 1 random number , loading random number (which not exist

is there better way of using random number load key , save keys?

if load , save calls happens in same browser session, , if randomnumberstringify key exists in scope accessible both of save , load functions, , not re-created again scope, should fine using approach.

however, trying use local storage persistent storege unit, means want access same data given user on different page loads or different browser session in which, in every load, javascript variable space gets erased.

if case latter one, should use hash functions generate keys, , use user dependent strings generate hash functions end generating same keys same users each time.

for example, given have sha1 hash function defined in javascript enviroment

var randomnumberstringify = sha1('username'); 

this key generation method allows same key same user everytime. , you'll have no problem setting , getting relevant data.

here :http://www.webtoolkit.info/javascript-sha1.html can find javascript implementation of hash function sha1.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -