This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The ChildNode.before method inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just before this ChildNode. DOMString objects are inserted as equivalent Text nodes.
[Throws, Unscopable] void ChildNode.before((Node or DOMString)... nodes);
HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy.var parent = document.createElement("div");
var child = document.createElement("p");
parent.appendChild(child);
var span = document.createElement("span");
child.before(span);
console.log(parent.outerHTML);
// "<div><span></span><p></p></div>"
var parent = document.createElement("div");
var child = document.createElement("p");
parent.appendChild(child);
child.before("Text");
console.log(parent.outerHTML);
// "<div>Text<p></p></div>" var parent = document.createElement("div");
var child = document.createElement("p");
parent.appendChild(child);
var span = document.createElement("span");
child.before(span, "Text");
console.log(parent.outerHTML);
// "<div><span></span>Text<p></p></div>" ChildNode.before() is unscopableThe before() method is not scoped into the with statement. See Symbol.unscopables for more information.
with(node) {
before("foo");
}
// ReferenceError: before is not defined You can polyfill the before() method in Internet Explorer 9 and higher with the following code:
// from: https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/before()/before().md
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('before')) {
return;
}
Object.defineProperty(item, 'before', {
configurable: true,
enumerable: true,
writable: true,
value: function before() {
var argArr = Array.prototype.slice.call(arguments),
docFrag = document.createDocumentFragment();
argArr.forEach(function (argItem) {
var isNode = argItem instanceof Node;
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.parentNode.insertBefore(docFrag, this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]); | Specification | Status | Comment |
|---|---|---|
| DOM The definition of 'ChildNode.before()' in that specification. | Living Standard | Initial definition. |
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Edge | Opera | Safari |
|---|---|---|---|---|---|---|
| Basic support | 54.0 | 49 (49) | No support | No support | 39.0 | No support |
| Feature | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Edge Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | ? | 49.0 (49) | No support | ? | 39.0 | No support | 54.0 |
ChildNode and ParentNode
ChildNode.after()ParentNode.append()Node.appendChild()Node.insertBefore()NodeList
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/before