Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attachShadow vs createShadowRoot

Tags:

shadow-dom

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?

like image 521
user2225055 Avatar asked Dec 24 '15 11:12

user2225055


People also ask

What is attachShadow?

attachShadow() method attaches a shadow DOM tree to the specified element and returns a reference to its ShadowRoot .

What is shadow DOM used for?

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.

What is shadow DOM in Salesforce?

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.

How do I get rid of shadow DOM?

In Chrome: Press F12, DevTool will open. Click gear icon in DevTool. Uncheck "show user agent shadow DOM" checkbox.


1 Answers

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 createShadowRootfor now. Once you start getting browser warnings for deprecation, that would be the time to move to attachShadow.

like image 100
Abhinav Avatar answered Oct 16 '22 13:10

Abhinav