TimelierSword
TimelierSword

Reputation: 61

Iframe ContentWindow

When I try to get the contentwindow out of an iframe, using

var contentWindow = document.getElementbyId('iframe').contentWindow 

sometimes it returns "Window undefined" because the contentWindow doesn't exist. I can't seem to run a check for it using

if (contentWindow === unidentified) or if (contentWindow === null)

as it just errors out of the code if I try to grab values out of it. Has anyone else run into this problem and figured out a solution?

Upvotes: 6

Views: 17588

Answers (3)

Decor
Decor

Reputation: 558

Try specifying what data you want from the object you call here

var contentWindow = document.getElementById('iframe')

by doing this

var contentWindow = document.getElementById('iframe').contentWindow

This worked for me

Upvotes: -2

meder omuraliev
meder omuraliev

Reputation: 186762

Are you typing it properly? getElementByID not getElementbyId. Did you confirm that that returns an element before querying contentWindow? Are you querying it after it loads?

Are you doing gEBI after DOM ready or page load? Did you throw in alerts on the element? Is the domain in the iframe the same as the origin domain? What browser are you using?

if (contentWindow === unidentified) or if (contentWindow === null)

There is no such thing as unidentified it's undefined. Slow down and be accurate.

Upvotes: 6

Wasim Karani
Wasim Karani

Reputation: 8886

Try this

var iframeElem = parent.document.getElementById("iframe");
var win = iframeElem.contentWindow;

Upvotes: 2

Related Questions