--- title: Node.setUserData() slug: Web/API/Node/setUserData translation_of: Web/API/Node/setUserData ---
Die Methode Node.setUserData()erlaubt es dem Benutzer Daten dem Element hinzuzufügen (oder löschen), ohne dabei die DOM zu modifizieren. Beachte dabei, dass die Daten durch das Nutzen von {{domxref("Node.importNode")}}, {{domxref("Node.cloneNode()")}} und {{domxref("Node.renameNode()")}} nicht mitgegeben werden kann (jedoch kann {{domxref("Node.adoptNode")}} die Information behalten), und bei Vergleichstest mit {{domxref("Node.isEqualNode()")}} werden die Daten nicht berücksichtigt.
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 {{domxref("Node.setUserData")}} methods are no longer available from Web content. {{domxref("Element.dataset")}} or WeakMap can be used instead.
var prevUserData = someNode.setUserData(userKey, userData, handler);
userKey is used as the key by which one may subsequently obtain the stored data. More than one key can be set for a given node.handler is 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 the handle method (part of the {{domxref("UserDataHandler")}} interface). 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 (null if being deleted), the destination node (the newly created node or null if none).If no handler is desired, one must specify null.userData is the object to associate to userKey on someNode. If null, any previously registered object and UserDataHandler associated to userKey on this node will be removed.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
| Spezifikationen | Status | Kommentar |
|---|---|---|
| {{SpecName('DOM WHATWG', '#interface-node', 'Node')}} | {{Spec2('DOM WHATWG')}} | Removed from the specification. |
| {{SpecName('DOM3 Core', 'core.html#Node3-setUserData', 'Node.setUserData()')}} | {{Spec2('DOM3 Core')}} | Initial definition |
{{CompatibilityTable}}
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | {{CompatNo}} | {{CompatGeckoDesktop("1.0")}} {{CompatNo}} {{CompatGeckoDesktop("22.0")}}[1] |
{{CompatUnknown}} | {{CompatNo}} | {{CompatNo}} |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | {{CompatNo}} | {{CompatGeckoMobile("1.0")}} {{CompatNo}} {{CompatGeckoMobile("22.0")}}[1] |
{{CompatVersionUnknown}} | {{CompatNo}} | {{CompatNo}} |
[1] The method is still available from within chrome scripts.