在此 Codelab 中,了解在访问 iframe 中的数据时,同源政策的运作方式。
设置:包含同源 iframe 的网页
此网页在同一源中嵌入了一个名为 iframe.html 的 iframe。由于主机和 iframe 共用相同的来源,因此主机网站能够访问 iframe 中的数据,并公开秘密消息,例如 blow。
const iframe = document.getElementById('iframe');
const message = iframe.contentDocument.getElementById('message').innerText;
更改为跨源 iframe
请尝试将 iframe 的 src 更改为 https://other-iframe.glitch.me/。托管页面是否仍能访问密文?
由于主机和嵌入的 iframe 没有相同的来源,因此对数据的访问受到限制。