Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

この記事はまだボランティアによって 日本語 に翻訳されていません。ぜひ MDN に参加して翻訳を手伝ってください!

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. Element.dataset or WeakMap can be used instead.

Syntax

var prevUserData = someNode.setUserData(userKey, userData, handler);

Parameters

  • 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).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.

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
DOM
The definition of 'Node' in that specification.
Living Standard Removed from the specification.
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Node.setUserData()' in that specification.
Recommendation Initial definition

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support Not supported 1.0 (1.7 or earlier)
Not supported 22.0 (22.0)[1]
? Not supported Not supported
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support Not supported 1.0 (1.0)
Not supported 22.0 (22.0)[1]
(Yes) Not supported Not supported

[1] The method is still available from within chrome scripts.

See also

ドキュメントのタグと貢献者

 最終更新者: Sebastianz,