Last active
May 13, 2019 13:34
Returns the unique identifier generated by the browser for the object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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