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

**URL:** <https://forum.kirupa.com/t/if-y-document-y-y-document/646147>\
**Category:** Uncategorized\
**Created:** [February 14, 2021, 5:16pm UTC](https://forum.kirupa.com/t/if-y-document-y-y-document/646147 "2021-02-14T17:16:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)\
**Post date:** [February 14, 2021, 5:16pm UTC](https://forum.kirupa.com/t/if-y-document-y-y-document/646147/1 "2021-02-14T17:16:27Z")

</div>

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 ?

```auto
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…

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 14, 2021, 8:54pm UTC](https://forum.kirupa.com/t/if-y-document-y-y-document/646147/2 "2021-02-14T20:54:37Z")

</div>

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 🤷‍♂️

🙂

---

<div class="post-metadata">

**Author:** ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)\
**Post date:** [February 17, 2021, 8:40pm UTC](https://forum.kirupa.com/t/if-y-document-y-y-document/646147/3 "2021-02-17T20:40:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 18, 2021, 5:29am UTC](https://forum.kirupa.com/t/if-y-document-y-y-document/646147/4 "2021-02-18T05:29:17Z")

</div>

The `y` variable would either equal `x.contentWindow.document` or `x.contentDocument`, whichever evaluated to **true** first 🙂
