同源策略 & iframe
通过集合保持井井有条 根据您的偏好保存内容并进行分类。
在此代码实验室中,了解在访问 iframe 内的数据时同源策略是如何工作的。
设置:具有同源 iframe 的页面
此页面嵌入了一个 iframe
,名为 iframe.html
,在同源中。由于主机和 iframe 共享相同的来源,因此主机站点能够访问 iframe 内部的数据并像下方这样公开秘密消息。
const iframe = document.getElementById('iframe');
const message = iframe.contentDocument.getElementById('message').innerText;
更改为跨域 iframe
尝试将 iframe
的 src
更改为 https://other-iframe.glitch.me/
。主机页面是否仍能访问秘密消息?
由于主机和嵌入的 iframe
没有相同的来源,因此对数据的访问受到限制。
除非另有说明,否则此页面的内容已获得 Creative Commons Attribution 4.0 许可 授权,代码示例已获得 Apache 2.0 许可 授权。有关详细信息,请参阅 Google Developers 网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
上次更新时间:2018-11-05 UTC。
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2018-11-05 UTC."],[],[]]