Link to home
Start Free TrialLog in
Avatar of PlumInternet
PlumInternet

asked on

JavaScript truncate variable value and save to localStorage

Hello,

We currently have a function that generates a unique ID (see below). I was wondering how we could update the function to take the last 20 digits of the unique id and push it into localStorage for the session. The function also needs to overwrite the unique id stored in localStorage if the function is execuited a 2nd or more times within the same session.

var _unqid = '' + Math.abs((function(s){return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);})(navigator.userAgent)) + "" + new Date().getTime() + "" + Math.floor(Math.random() * 1000);
var x =_unqid;

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PlumInternet
PlumInternet

ASKER

Perfect, worked like a charm thanks for your help.