Skip to content

Instantly share code, notes, and snippets.

@renaatdemuynck
Last active May 13, 2019 13:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renaatdemuynck/57b1b78b6611e6d5d866 to your computer and use it in GitHub Desktop.
Save renaatdemuynck/57b1b78b6611e6d5d866 to your computer and use it in GitHub Desktop.
Returns the unique identifier generated by the browser for the object
/**
* @file Returns the unique identifier generated by the browser for the object.
* The uniqueID property of the document works as an identifier generator in Internet Explorer.
* Each time when the value of this uniqueID property is retrieved, the browser generates
* a new unique identifier and returns it.
* HTML elements also support the uniqueID property. The first time when the value of any of these
* properties is retrieved, the browser generates a new identifier for the element.
* @author Renaat De Muynck <renaat.demuynck@gmail.com>
*/
(function () {
'use strict';
var nextID = 1;
if (Document.prototype.hasOwnProperty('uniqueID')) {
return;
}
console.info('"document.uniqueID" not implemented; creating shim');
// define a property on the document that will generate a unique id each time it is called
Object.defineProperty(Document.prototype, 'uniqueID', {
get: function () {
return nextID++;
},
enumerable: false,
configurable: false
});
// define a property on the element that will return a unique id the first time it is called
Object.defineProperty(Element.prototype, 'uniqueID', {
get: function () {
// redefine the property so that the generator will not be called again
Object.defineProperty(this, 'uniqueID', {
value: document.uniqueID,
writable: false,
enumerable: false,
configurable: false
});
return this.uniqueID;
},
enumerable: false,
configurable: true
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment