使用 srcdoc 调整 <amp-iframe> 的大小?
Resizing an <amp-iframe> using srcdoc?
是否可以在使用 srcdoc
属性时创建可调整大小的 amp-iframe
?对于要调整大小的 amp-iframe
,docs say the following:
- The amp-iframe must set the allow-same-origin sandbox attribute.
- The amp-iframe must be defined with the resizable attribute.
- The amp-iframe must have an overflow child element.
- The iframe document must send an embed-size request:
然后,调度以下事件:
window.parent.postMessage({
sentinel: 'amp',
type: 'embed-size',
height: document.body.scrollHeight
}, '*');
但是,当我尝试将 allow-same-origin
属性添加到我的 iframe 时,我在控制台中收到以下错误:"allow-same-origin is not allowed with the srcdoc attribute."
出于安全考虑,这似乎是设计使然,至少根据代码 here,因为 srcdoc
不会受到跨源域保护的约束。
这是否意味着无法动态调整通过 srcdoc
填充的 amp-iframe
的大小?
我是否遗漏了有关如何执行此操作的一些基本知识?
你说得对 - 目前无法通过 srcdoc 调整大小。如果您认为在没有 allow-same-origin
的情况下调整大小应该没问题,我建议您在 Github.
上提交功能请求
是否可以在使用 srcdoc
属性时创建可调整大小的 amp-iframe
?对于要调整大小的 amp-iframe
,docs say the following:
- The amp-iframe must set the allow-same-origin sandbox attribute.
- The amp-iframe must be defined with the resizable attribute.
- The amp-iframe must have an overflow child element.
- The iframe document must send an embed-size request:
然后,调度以下事件:
window.parent.postMessage({
sentinel: 'amp',
type: 'embed-size',
height: document.body.scrollHeight
}, '*');
但是,当我尝试将 allow-same-origin
属性添加到我的 iframe 时,我在控制台中收到以下错误:"allow-same-origin is not allowed with the srcdoc attribute."
出于安全考虑,这似乎是设计使然,至少根据代码 here,因为 srcdoc
不会受到跨源域保护的约束。
这是否意味着无法动态调整通过 srcdoc
填充的 amp-iframe
的大小?
我是否遗漏了有关如何执行此操作的一些基本知识?
你说得对 - 目前无法通过 srcdoc 调整大小。如果您认为在没有 allow-same-origin
的情况下调整大小应该没问题,我建议您在 Github.