browser-uid
Version:
To get the individual browser ID (which typically means identifying a unique session or user/browser), we would generally need a way to distinguish between users across different sessions. In a browser environment, this can be achieved with cookies or ses
26 lines (20 loc) • 707 B
JavaScript
// index.js
// Utility to generate a unique identifier (UUID)
function generateUUID() {
return 'xxxxxxxxyxxxxyxxxxxxxxxxxxxxxxyxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// Function to get a unique browser ID
function getBrowserID() {
// Try to retrieve from localStorage
let browserId = localStorage.getItem('browser-id');
if (!browserId) {
// If it doesn't exist, create one and store it in localStorage
browserId = generateUUID();
localStorage.setItem('browser-id', browserId);
}
return browserId;
}
export default getBrowserID;