How to Access iframe from Parent Element – Best Methods? or What's the Proper Way to Access iframe fr

18 Replies, 1373 Views

Man, this is such a common headache. For same-origin, `contentWindow` *should* work, but timing is everything. If the iframe isn't loaded, you'll get `null`.

Try this:

```js
const iframe = document.getElementById('your-iframe');
iframe.onload = function() {
const innerDoc = iframe.contentDocument || iframe.contentWindow.document;
console.log(innerDoc.body);
};
```

Cross-origin? Forget it. Security rules are strict for a reason.

Messages In This Thread



Users browsing this thread: 1 Guest(s)