First, I know there are many questions about this phenomenon but none of the workarounds given so far would help in the situation I have here.
Environment: My code is in the Outer iFrame inside a page I don't control. It's sized to fit all my content so no scrolling happens inside my iFrame. Rather, the outer page can scroll it up and down to view its content.
Problem: I have an inner iFrame holding third party ads which I can't control. If the inner iFrame runs any of the following Javascript commands, the outer page scrolls everything down to bring the inner iFrame into view:
window.location.href or document.location.hash to an anchor inside it.
scrollIntoView on an element inside it
Here is a demo a colleague put together (wait 5 seconds for it to start)
Solutions that don't work:
How to Disable Scrolling on an <iframe> Add the scrolling="no" attribute to the <iframe> element to prevent users from scrolling on an iframe, both horizontally and vertically.
You can set sandbox="" , which prevents the iframe from redirecting. That being said it won't redirect the iframe either. You will lose the click essentially. Save this answer.
If you don't want the contents of the iframe to be interactable by the user, you can disable pointer-events on it. But as you want it to be scrollable, just put a full sized iframe in a smaller div with overflow: scroll.
You need to know the content height of the page that was loaded into the iframe and adjust the iframe correspondently. Fill up the iframe inside the content div. Hide the scroll bar in the iframe, but detect scroll somehow. However this will be a daunting task and the road to this will be full of potholes.
Maybe there are some other ways to solve this issue, but one pretty straightforward solution is to take troublesome iframes out of the natural layout of the site by positioning them as fixed
.
Then the only remaining challenge may be to force them to behave as if they were part of the natural layout. But this tasks seems to be of mediocre complexity in comparison to the original problem.
Moreover, in your particular case, the above approach works pretty out of the box. Since the outer iframe is not scrollable and its height matches its content height: you just need to insert -- for each iframe -- a placeholder that matches its counterpart size...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With