Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a browser supports shadow DOM

One way would be to check if there is a .shadowRoot property on an element, however I need to return a boolean before the page is rendered.

like image 557
offthegrass Avatar asked Apr 19 '15 13:04

offthegrass


People also ask

Do all browsers support Shadow DOM?

Shadow DOM (V1) on Android Browser is fully supported on 97-103, partially supported on None of the versions, and not supported on 2.3-4 Android Browser versions. Shadow DOM (V1) on Opera Mobile is fully supported on 64-64, partially supported on None of the versions, and not supported on 10-12 Opera Mobile versions.

How do I check Shadow DOM?

To identify Shadow DOM:Open Developer tools (press the shortcut keys Fn+F12). On the Elements tab, expand the <body> element and the first element inside the <body> element and notice the #shadow-root line.

How do I inspect Shadow DOM in Chrome?

Open Dev tool by pressing F12. Click on the cog icon in the right top. Enable option "Show Shadow DOM" under Elements category. Go to Elements panel and you will see the component DOM with all styles.

Does EDGE support Shadow DOM?

Feature detection and browser support #Declarative Shadow DOM is available in Chrome 90 and Edge 91.


1 Answers

One simple feature test would be:

if (document.head.createShadowRoot || document.head.attachShadow) {
    // I can shadow DOM
} else {
    // I can't
}

This will work even if you include the script in the head section and assumes no malicious scripts were added prior to yours (a safe assumption).

Currently, Chrome, Opera, and derived browsers (like Android browsers) support it. For more information, visit: https://caniuse.com/#feat=shadowdomv1 and http://caniuse.com/#feat=shadowdom

like image 82
Benjamin Gruenbaum Avatar answered Sep 20 '22 12:09

Benjamin Gruenbaum