How to check if a browser supports shadow DOM

安稳与你 提交于 2020-01-21 10:50:26

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/29730398/how-to-check-if-a-browser-supports-shadow-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!