在此 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
的来源不同,因此对数据的访问会受到限制。