--- url: "https://web.dev/articles/codelab-same-origin-iframe" titulek: "Same Origin Policy & iframe | Articles | web.dev" h1: "Same Origin Policy & iframe Stay organized with collections Save and categorize content based on your preferences." popis: "In this codelab, learn how the same-origin policy works when accessing data inside an iframe." jazyk: "en" kanonicka: "https://web.dev/articles/codelab-same-origin-iframe" drobecky: ["Articles", "Same Origin Policy & iframe"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 02:42:11" znaku: 1336 slov: 190 kodovani: "iso-8859-2" sha256: "a6b52ebed171978d4ccd3a6bc9dac2625240ab3b227acac7b84ca7a3673f799f" --- Same Origin Policy & iframe Stay organized with collections Save and categorize content based on your preferences. Mariko Kosaka In this codelab, see how the same-origin policy works when accessing data inside an iframe. Set up: Page with a same-origin iframe This page embeds an `iframe`, called `iframe.html`, in the same origin. Since the host and the iframe share the same origin, the host site is able to access data inside of the iframe and expose the secret message like blow. `const iframe = document.getElementById('iframe'); const message = iframe.contentDocument.getElementById('message').innerText;` Change to cross-origin iframe Try changing the `src` of the `iframe` to `https://other-iframe.glitch.me/`. Can the host page still access the secret message? Since the host and embedded `iframe` do not have the same origin, access to the data is restricted. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2018-11-05 UTC.