Anders
Anders

Reputation: 521

How to get div width from within an iframe? Jquery

How would I go about getting the height of a div within an iframe. Right now I am using jquery, and document ready. Here's the code I have right now.

$( document ).ready(function() {
console.log($(".cf_offers").height());  
});

The div of the iframe is content_iframe, but is it needed? Because the div has the width when the page is ready. But it just outputs null.

Any help would be appreciated!

Upvotes: 0

Views: 689

Answers (1)

adeneo
adeneo

Reputation: 318182

If you're not violating the same-origin policy, you have to use contents() to get the contents of the iFrame and find elements etc.

The iFrame would have to be loaded, but if that's not an issue, you can remove the onload handler :

$("#content_iframe").on('load', function() {
    var width = $(this).contents().find(".cf_offers").height();
});

Upvotes: 1

Related Questions