? if (y.document)y = y.document; ?

Hello ;
I am having trouble understanding lines 2 and 3 below:
how would I read it out loud in a sentence what is going on ?

var x = document.getElementById("myframe");
var y = (x.contentWindow || x.contentDocument);
if (y.document)y = y.document;
y.body.style.backgroundColor = "red";

Also can you aim me at an iframes tutorial , “How much can the parent ,query and control , what’s going on in child” .
Thanks 2 times…

You are targeting an iframe element with x. With these lines…

var y = (x.contentWindow || x.contentDocument);
if (y.document)y = y.document;

…what you are doing is accessing the iframe’s document object and storing it on the y variable. The two ways you can access an iframe’s document object is by x.contentWindow.document or directly via x.contentDocument.

Older IE browsers don’t support contentDocument directly, so they only support it via contentWindow.document. This code tries to account for that with the if statement where it checks if y.document (basically x.contentWindow.document) exists or not. If it does, then ensure y is set to it. If this statement fails for some reason, the y is set to x.contentDocument from the earlier line.

With that said, the if statement is unnecessary afaict. The code can be written as:

var y = (x.contentWindow.document || x.contentDocument);

This ensures whichever part of the condition evaluates to true is what gets set to y :man_shrugging:


Thank you
if (y.document)y = y.document;
It thru me I am used to if (y.document) {y = y.document;} ;
var y = (x.contentWindow.document || x.contentDocument);
If y did equal one of those , what would that look like ?
Thanks again !

The y variable would either equal x.contentWindow.document or x.contentDocument, whichever evaluated to true first :slight_smile: