1

I want to set the height of the iframe match it's content. But iframe.contentWindow returns undefined even after putting it inside .load(). Here's the code:

$(document).ready(function() {
    var myIFrame = $('#iframe');
    console.log(myIFrame.contentWindow); //getting undefined
});

Can someone tell me what's wrong in this? Thanks a lot.

Chaitanya
  • 719
  • 9
  • 23
  • After `var inst = this.handle` in `setAutoHeight`, Could you try `console.log(inst)`? – Yonggoo Noh Feb 18 '18 at 14:48
  • `console.log(inst)` isn't printing anything. `alert(inst)` is getting `[object Object]` @YonggooNoh – Chaitanya Feb 18 '18 at 14:53
  • I think you should check the `inst` in debugging mode. – Yonggoo Noh Feb 18 '18 at 14:55
  • In debugging mode, `console.log(inst)` is printing `Object [ iframe#iframe-main ]`. I guess the `.load` function is firing off before the `iframe` actual loading because the `alert(doc)` displays `undefined` before there's any visible content of the `iframe`. @YonggooNoh – Chaitanya Feb 18 '18 at 15:07
  • `load` event will be fired when the iframe contents is **actually** loaded. Could you give me a sample JSFiddle page? – Yonggoo Noh Feb 18 '18 at 15:16

1 Answers1

2

Alright, so contentWindow is native Javascript and since I'm using jQuery, I'll have to get the original DOM object first. The correct way to get contentWindow :

var win = $('#iframe').get(0).contentWindow;

For More Information

Chaitanya
  • 719
  • 9
  • 23