JavaScript Get iframe DOM Element

Apr 16, 2020
// get iframe DOM elementconst el = document.getElementsByTagName('iframe')[0].contentDocument// query element within iframeconst abbrEl = getElementsByTagName('abbr')[0]

If the iframe is from a different domain/orign, you will bump into Uncaught DOMException: Blocked a frame with origin "http://..." from accessing a cross-origin frame.

If you own both the main page and iframe page, you could use the window.postMessage hack.

Alternatively, you can disable same-origin policy in your browser.

NOTE: Remember not to use the browser for normal browsing due to security risk

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.