iframe allow 属性

1,178 阅读1分钟

最近有一个需求需要在自己的网站上内嵌一个 iframe。我们自己的网站是 a.b.com. 内嵌的 iframe 我们称为 c.b.com 。内嵌的 iframe 有开启用户麦克风的功能。

为了能够让 iframe 拥有麦克风权限。我们通常会在我们自己开发的html里面这么写

<iframe src="https://c.b.com" allow="microphone *"></iframe>

这样,c.b.com 就可以拥有麦克风权限了。

但我在实际项目中用的时候,一直拿不到权限,最终定位 bug 出现的原因是是,我是用 js 脚本手动向文档里面插入的 iframe 节点,等 iframe 节点插入到文档中后,再设置的 allow 属性。

而allow 属性必须在 iframe 插入文档之前就设置好才会起作用。