I read in mozilla doc, Element.createShadowRoot() is deprecated:
This method has been deprecated in favor of attachShadow.
But in my canary: chrome 49.0.2599.0
thats work:
var shadow = document.getElementById("node-sh").createShadowRoot();
And thats not work
var shadow = document.getElementById("node-sh").attachShadow({mode: 'closed'});
Anyone know what's right?
attachShadow() method attaches a shadow DOM tree to the specified element and returns a reference to its ShadowRoot .
Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with a shadow root, underneath which you can attach any element, in the same way as the normal DOM.
Shadow DOM is a standard that encapsulates the internal document object model (DOM) structure of a web component. Encapsulating the DOM gives developers the ability to share a component and protect the component from being manipulated by arbitrary HTML, CSS, and JavaScript.
In Chrome: Press F12, DevTool will open. Click gear icon in DevTool. Uncheck "show user agent shadow DOM" checkbox.
createShadowRoot()
is the old way of attaching a shadow-root to host element. It was proposed in initial spec, which has then been deprecated in favor of
attachShadow
.
Spec also has been updated.
But, new API hasn't been standardized and none of the browsers currently support it. So I would suggest to stick to createShadowRoot
for now. Once you start getting browser warnings for deprecation, that would be the time to move to attachShadow
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With