views:

357

answers:

1

I've a site with banner ads, loaded using iframes. The banner ads always scroll the parent window when the banner doesn't complety fit into the visible region. This is really annoying and I'd like to deny the javascript of the iframes to scroll the main window.

Here is what I already tried:

  1. Move the iframe src code to another domain. Normally js from another domain should not be allowed to access the parent window, is it?! This doesn't work.

  2. Overwrite window.moveTo, window.scrollTo, window.scrollBy in parent with my own functions. This doesn't seem to work neither, as none of these functions seems to be called. :-(

Any help would be really great :-)

A: 

Give the iframe a width and set a style for it like overflow: hidden;?

npup
The iframe already has a fixed with and height. But anway, how should this help in preventing scrolling of the parent window?
gucki
Excuse me if I misunderstood your setup, but if you have an element with fixed dimensions and overflow hidden, as far as I know it should stay PUT whatever is rendered in it. I now understand there is some javascript in the iframe-d source that reaches out for your window and makes it scroll programmatically. Correct?
npup
The page loaded inside the iframe is from the ads provider and completely out of my control. This code seems to call some javascript code which makes the parent window scroll so that the iframe is always completely in the viewport. I want to prevent this scrolling from happening.
gucki
I now solved it by placing onload="window.scrollTo(0,0);" in the iframe tag. But this causes flickering, which is also annoying. Any better solution would be great :-)
gucki
I'm afraid it's the best you can do, really. Seems one cannot capture and cancel a scroll event: http://www.w3.org/TR/2009/WD-DOM-Level-3-Events-20090908/#event-type-scroll (this was mentioned in #1459676).Were it cancel-able we could just have done something like `window.addEventListener('scroll', function(e) {e.preventDefault();e.stopPropagation();e.stopped=true;}, false);`
npup
I tried this too (putting window.scrollTo(0,0) inside the window.onScroll event), but the problem with this approach is that the event is also triggered when the user scrolls manually. If I could somehow distinguish between "automatic" scroll and user scroll...this would be great :-)
gucki