Node.setUserData

Table of contents

  1. 1. Summary
  2. 2. Syntax
  3. 3. Example
  4. 4. Specification
  5. 5. See also
Table of contents
  1. 1. Summary
  2. 2. Syntax
  3. 3. Example
  4. 4. Specification
  5. 5. See also

« Gecko DOM Reference

Summary

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 importNode, as with cloneNode() and renameNode() operations (though adoptNode does preserve the information), and equality tests in 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.

Syntax

prevUserData = someNode.setUserData(userKey, userData, handler);
  • someNode is a node, such as an element.
  • 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 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). See UserDataHandler. 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.
  • prevUserData is the user data previously stored on the node, and null if none.

Example

var d = document.implementation.createDocument('', 'test', null); 
d.documentElement.setUserData('key', 15, {handle:function (o, k, d, s, ds) {alert(o+'::'+k+'::'+d+'::'+s+'::'+ds)}}); // 2::key::15::[object Element]::[object Element]
alert(d.documentElement.getUserData('key')); // 15
var e = document.importNode(d.documentElement, true); // causes handler to be called
alert(e.getUserData('key')); // null since user data is not copied

Tags (3)

Edit tags

Attachments (0)

 

Attach file