Obsolete
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
The Node.setUserData() method allows a user to attach (or remove) data to an element, without needing to modify the DOM. Note that such data will not be preserved when imported via Node.importNode, as with Node.cloneNode() and Node.renameNode() operations (though Node.adoptNode does preserve the information), and equality tests in Node.isEqualNode() do not consider user data in making the assessment.
This method offers the convenience of associating data with specific nodes without needing to alter the structure of a document and in a standard fashion, but it also means that extra steps may need to be taken if one wishes to serialize the information or include the information upon clone, import, or rename operations.
The Node.getUserData and Node.setUserData methods are no longer available from Web content. HTMLElement.dataset or WeakMap can be used instead.
Syntax
var prevUserData = someNode.setUserData(userKey, userData, handler);
Parameters
userKeyis used as the key by which one may subsequently obtain the stored data. More than one key can be set for a given node.handleris a callback which will be called any time the node is being cloned, imported, renamed, as well as if deleted or adopted; a function can be used or an object implementing thehandlemethod (part of theUserDataHandlerinterface). The handler will be passed five arguments: an operation type integer (e.g., 1 to indicate a clone operation), the user key, the data on the node, the source node (nullif being deleted), the destination node (the newly created node ornullif none).If no handler is desired, one must specifynull.userDatais the object to associate touserKeyon someNode. Ifnull, any previously registered object and UserDataHandler associated touserKeyon this node will be removed.
Example
var d = document.implementation.createDocument('', 'test', null);
d.documentElement.setUserData('key', 15, {handle:function (o, k, d, s, ds) {console.log(o+'::'+k+'::'+d+'::'+s+'::'+ds)}}); // 2::key::15::[object Element]::[object Element]
console.log(d.documentElement.getUserData('key')); // 15
var e = document.importNode(d.documentElement, true); // causes handler to be called
console.log(e.getUserData('key')); // null since user data is not copied
Specifications
| Specification | Status | Comment |
|---|---|---|
| Document Object Model (DOM) Level 3 Core Specification The definition of 'Node.setUserData()' in that specification. |
Obsolete | Initial definition |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
setUserData | Chrome No support No | Edge No support No | Firefox No support 1 — 22 | IE ? | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android No support 4 — 22 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No |
Legend
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.
