4

An ad provider wants us to add some Javascript to our site that'll allow them to resize the iframe their ad is served into. I've been going through the code, and part of it is this loop:

var topIframes = top.document.getElementsByTagName('IFRAME');
for (var i = 0; i < topIframes.length; i++) {
    if (topIframes[i].contentWindow === self) {
        // found iframe that served the ad
        topIframes[i].style.height = sz + 'px';
    }
}

I can see it's grabbing all the iframes in the document and adjusting the height of one or more of them. But I can't figure out what the condition's doing.

I know contentWindow's the window inside an iframe, and looking at What's the difference between self and window? I see that "self" is a reference to the window object. But which window object? The parent window or the window inside the iframe? Is there even a window inside the iframe? Why check that the window inside an iframe is the window inside an iframe?

////////////////////////////////////////////

EDIT

At Snuffleapagus's request, here's the long version:

<script type="text/javascript">

    // iframe shrink function that needs to be on the hosting page
    rp_resize = function (sz) {
        try {
            var topIframes = top.document.getElementsByTagName('IFRAME');
            for (var i = 0; i < topIframes.length; i++) {
                if (topIframes[i].contentWindow === self) {
                    // found iframe that served the ad
                    topIframes[i].style.height = sz + 'px';
                }
            }
        } catch (e) {
        }
    }

</script>

<script>

    // this is the code that goes in the passback to initiate the function
    try {
        if (typeof(rp_mpu) === 'function') {
            rp_resize(250);
        }
    } catch (e) {

    }

</script>

<script language="JavaScript" type="text/javascript">
rp_account   = '<account-id>';
rp_site      = '<site-id>';
rp_zonesize  = '<zone-id>-<size-id>';
rp_adtype    = 'js';
rp_smartfile = 'http://<url>/..../revv_smart_file.html';    // this should be the URL path to the friendly iframe that needs resizing
</script>
<script type="text/javascript" src="http://ads.<url>.com/ad/<account-id>.js"></script>

////////////////////////////////////////////

EDIT

Here's a possible clue from the ad provider in answer to my question about the condition. Don't know how much use it is, as he's not a developer.

"The line of code you are looking at is trying to determine if it is the iFrame from which the function has been initiated so it can be resized accordingly."

Community
  • 1
  • 1
And Finally
  • 5,602
  • 14
  • 70
  • 110
  • Im not 100% which is why this is a comment, but it seems to me it is checking if the window of the iframe is the same as the current window, but why that would ever be true is what confuses me. Perhaps the rest of the code holds some hints? – musefan Nov 05 '13 at 17:02
  • Do you have any more code to show? `.contentWindow` of an iframe refers to the window object of the iframe itself so I'm not sure why it would ever be equal to the normal window object. The other bothersome part is where is the `sz` variable coming from? – Snuffleupagus Nov 05 '13 at 17:04
  • The style of the iframe can't be changed with a reference to a window, a reference to the iframe itself is needed, and that reference is held in parent. This code is searching, from the point of view of root of frameset (the top property) to find the iframe reference where the current window (current is the context in where the javascript code runs, so self) is defined, and once the reference is found, style is changed. See [this](http://msdn.microsoft.com/en-us/library/ms952669.aspx) – MC ND Nov 05 '13 at 17:10
  • @MCND: But surely if this code is run in the OP's site (which isn't inside an IFRAME) then `top` is actually the same as `self` anyway? It would make more sense if the code was run inside the IFRAME, but it is not – musefan Nov 05 '13 at 17:14
  • @musefan: Where is said there is no iframe? how do you know where the code is running? and i don't know if the ad provider (in the OP question) is not generating his own iframe. But it's ok, if there isn't any type of frame/iframe, yes window.top = window.self – MC ND Nov 05 '13 at 17:20
  • Thanks guys for thinking about this - I was interpreting this a similar way, but I reckon Charlie74 has hit on the solution. It seems that rp_resize is called from inside the iframe, and this loop finds which page called it. – And Finally Nov 05 '13 at 18:11
  • @MCND: The OP said that they have been given this code to run in their own page (i.e. the page that hosts the iframe), the only thing that makes sense is either the OP is not telling us everything, or the people who gave the OP that script do not understand it, or have left it in there by mistake – musefan Nov 06 '13 at 09:13
  • @musefan: no, what makes sense is that the people who write this code thought they don't know how the hosting page is organized or what it contains (the need to iterate iframe collection), and it's not possible to resize the iframe from inside itself and from content in another domain, BUT placing code in parent window, and calling it from inside the iframe, they get code running in the context of the iframe window (so self is the iframe window, not parent), but the code is placed in parent page, handling cross domain restrictions in code execution. – MC ND Nov 06 '13 at 09:49
  • @MCND: Ah, well now that makes more sense if it is indeed true. `self` referring to the iframe "window" would make sense. You should add that as an answer, because the accepted one makes no attempt to explain that key fact – musefan Nov 06 '13 at 09:55

3 Answers3

1

From what I understand working with Javascript and how it can access iFrames, the provider is assuming that you have multiple iFrames on the page. Also, it assumes that the iFrame they are looking for does not have an ID to reference easily.

Based on this, after the frame with the ad content loads, at some point it will call rp_resize(250);. However, the function rp_resize does not know which of the iFrames on the page it was called from. The script loops through all the iFrames on the page until it finds the one that called the function. This is how it knows which frame to call.

Hopefully that makes sense and / or answers your question.

Charlie74
  • 2,883
  • 15
  • 23
  • This does not make sense! You are suggesting that the script is being run from within the ad providers iframe, but the OP has said they have been given this script to include in their own page (the one that hosts the iframe). It would make perfect sense running in the ad iframe, but it this context it does not – musefan Nov 06 '13 at 09:11
  • @musefan Correct. The script IS being launched from within the ad provider's iFrame. However, the iFrame does not have the capability of changing it's own size. Because of this, the script needs to be launched at the page level. The script then finds the appropriate iFrame, and changes its display size. – Charlie74 Nov 06 '13 at 17:31
0

I think, self refers to the parent window. To check, type the following in your browser console and see the result :

self == window
aash
  • 1,323
  • 1
  • 14
  • 22
  • 1
    indeed this is true, but the question is about why the condition is being performed, not what `self` refers to... although I suppose it is part of the confusion – musefan Nov 05 '13 at 17:06
0

.contentWindow will return null if the iframe hasn't completely loaded. It looks like the code is looping through iframes, checking if they are loaded, and if so, resizing them.

Edit: musefan is right; I worded it incorrectly.

Edit 2: Why check that the window inside an iframe is the window inside an iframe? It's null if it's not loaded yet; if it is loaded, it's a window.

  • 1
    `.contentWindow === self` is an equality check and will only ever return true or false. `.contentWindow` might return null though, so I assume that is what you mean – musefan Nov 05 '13 at 17:19