MoreLesson 288Free preview
Get Iframe Elements
This lesson is part of How To · All levels
Summary
Iframe allows one page to display another document inside a small box. JavaScript can access iframe content via contentDocument or contentWindow, but only if the page comes from the same source. For cross-origin iframe, communication must be made with postMessage because the browser blocks direct access for security reasons.
Key points
- Iframe.contentDocument gives access to documents within the iframe
- Iframe.contentWindow refers to the iframe window object
- Same-origin policy prohibits DOM access across domains
- PostMessage is a secure way to communicate between origins
- Handle the event load so the script runs after the iframe content is ready
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.