Universally unique identifier

Sometimes when you submit data through a form, you might need to have an unique identifier. Incrementing a value each time you submit the form might not be a choice if your communication with the database is only one way – POST. We would have to create an universal unique identifier – a string that we are sure that would never be repeated. Here is a function that helped me solve this problem once:

function generateUUID() {
	var d = new Date().getTime();
	var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		var r = (d + Math.random()*16)%16 | 0;
		d = Math.floor(d/16);
		return (c=='x' ? r : (r&0x7|0x8)).toString(16);
	});
	return uuid;
};

You can easily create a variable from here which is going to hold your uuid

var sessionUniqueId = generateUUID();

store it in a cookie, POST it with AJAX…

Leave a Reply

Your email address will not be published. Required fields are marked *