Scrolling inside an iFrame
Here's the deal: I spent 5 hours looking for a good solution for my problem and found many (unfortunately not working, or it was me doing sth wrong).
I have a page, in which I have an iFrame. I do not want to scroll the page to see the iFrame properly. My point is to scroll the iFrame in that page so it would change it's position (page inside iFrame).
What won't work: putting a window scrollTo into a iFrame page code (simply because this page is not mine, so i.e.: I have my own page and in iFrame I have stackoverflow.com, and I want it to scroll 100px to the right and 500px to the bott开发者_StackOverflow社区om).
I hope you got my point, and will help me to fix it.
Just please, be kind and give me the whole working example with an iFrame. I just can't stand spending any more time on figuring it out... Is it even possible? ...
Pekka is correct - it's not possible. Because the iframe's src
is from a different domain, no browser will let code from the parent window access the DOM or window. This is important and intentional. If pages are from different domains, it's entirely possible that a malicious script running in a separate window could cause serious problems in the parent window.
One way to do it is as follows:
<div id="fakeiframe" style="height: 500px; width: 800px;
overflow-y: scroll; overflow-x: hidden;">
<iframe src="http://www.washingtonpost.com/"
style="width: 1500px; height: 2000px;"></iframe>
</div>
The div
size should be smaller than the iframe
size. Then, you can scroll the "iframe" using document.getElementById('fakeiframe').scrollTo(x,y);
You can modify the CSS to tweak the frame to your needs.
精彩评论