Assuming the key in localStorage is "username":
document.cookie='username=' + localStorage.getItem('username') + 'expires='+new Date((new Date().getTime()) + 1000*60*60*24*7).toGMTString() +'; path=/';
This cookie expires in 7 days. Change "7" (there is only one) for as many days as you like. To save other keys, simply change the two occurrences of the "username" in the above code to what the variable name is (for example, "password").
source share