如何检查浏览器是否支持shadow DOM [英] How to check if a browser supports shadow DOM

查看:773
本文介绍了如何检查浏览器是否支持shadow DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一种方法是检查元素上是否有.shadowRoot属性,但是我需要在页面呈现之前返回一个布尔值。

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.

推荐答案

一个简单的功能测试将是:

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).

目前,Chrome,Opera和派生浏览器(如Android浏览器)支持它。
有关详细信息,请访问: http://caniuse.com/#feat=shadowdom

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

这篇关于如何检查浏览器是否支持shadow DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆