Search
Learn
Share

JavaScript: Get content of an iframe

Published by Profile Image LGK on 4 September, 2017
9 views
Sometimes you need to get the content of an HTML iframe.
Here is how you can do it.

But you can only access the content when it's on the the same domain!

var iframe = document.querySelector("iframe");

// Make sure the iframe is ready
iframe.onload = function() {
var iframeContent = iframe.contentDocument || iframe.contentWindow.document;

// Now you can make normal queries
console.log(iframeContent.querySelector("#element-in-iframe").innerText);
};