views:

939

answers:

4

On my computer, I'm using a custom adblock based on HOSTS redirection (so I can have adblock in Chrome). I'm running IIS on my machine, so I have a custom blank 404 error that displays when an iframe-based ad is displayed on a page.

I've modified my 404 error to inherit its background color from its parent so that the ad doesn't look obnoxious on sites with a non-white background. My next challenge is to use my 404 page to completely collapse the iframe so that it doesn't display on the page at all.

Is it possible to alter the containing <iframe /> tag from within the iframe? I just want to change the height & width attributes if this is possible. If so, how would I go about doing this?

+1  A: 

Shouldn't be possible, as sites within iframes can be external site. Allowing them to manipulate the environment they're displayed in would be a browser security risk.

You'd be better off using an adblocking proxy.

ceejayoz
Unless I'm missing something, an adblocking proxy would be basically the same as what I'm using now. I'm just trying to figure out a way to collapse the IFRAME area, but it does make sense that it's not possible.
Dan Herbert
The adblocking proxies I've seen remove the ad code from the webpage instead of blocking the IP/URL. The iframe never exists, as it gets stripped out.
ceejayoz
A: 

If I understand you correctly, you're saying you want your 404 page to trigger a collapse of the iframe when it loads inside it. The easiest way to do this is to create a function in the parent page, and then call it from the 404 when it loads.

For example, if your iframe has the id "advertFrame" you could add the following javascript function to the parent page:

function hideAdvertFrame() {
  var advertFrame = document.getElementById("advertFrame");
  advertFrame.style.height = 0;
  advertFrame.style.width = 0;
}

Then in the 404 page, you can call the function immediately when it loads:

<body onload="window.parent.hideAdvertFrame();">

That said, I think you still might want to consider other options besides cross frame scripting that are less prone to security holes. Maybe your parent page can use AJAX to check if the adblock will have a valid URL before it loads, and then dynamically add it to the page once certain.

David Crow
That would be a good idea; unfortunately, I have no control over the parent pages. I only have control over the IFrame 404 source. After doing some research, I found that (if the security mechanisms weren't there) there is actually a JavaScript property I could access to change the IFrame easily.
Dan Herbert
A: 

The page that fills the iframe cannot alter anything about the page that contains the iframe. If it could, then it would be a huge security risk. To be sure, I tried just what you're asking using the two domains that I'm given at school. Simply put, it didn't work; I got a permission denied error in Firebug.

geowa4
A: 

But what if we use same domain ? I mean the iframe is on the same domain as the parent ?

Keepwalking
Then you can modify the iframe with JavaScript. You can only access the parent page if you're on the same domain. If you're on a sub-domain, the browser considers the domain different and will block access.
Dan Herbert