Is it possible to scroll the iframe window from the parent window using keys or javascript? The iframe content is from another domain, different from the parent window.
Asked
Active
Viewed 2.1k times
1 Answers
20
Since the iframe content is from another domain, you will not be able to alter it's DOM due to security reasons.
Although you can scroll it by using the arrow keys, when you have it activated. At least it works for me in Chrome and Firefox.
If you want to be able to scroll it from javascript, I would suggest the following approach. (it assumes you know the width and height of the iframe content and your iframe). Basically let a div in your DOM take care of scrolling.
<a href="#" id="scroll">Scroll to (400,400)!</a><br />
<div id="google" style="width: 300px; height: 200px; overflow: auto;">
<iframe width="800" height="600" src="http://www.google.com/" scrolling="no">
</iframe>
</div>
<script type="text/javascript">
$("#scroll").click(function()
{
$("#google").scrollTop(400).scrollLeft(400);
return false;
});
</script>
For smoother scrolling of the div you could try the code from this article.

Martin Nycander
- 1,309
- 13
- 29
-
So, is there a way we can find out the height of the site inside the iframe? Thanks for the reply. Really appreciate it :) – Mickey Cheong Nov 01 '09 at 11:48
-
Not that I know of, see http://www.webmasterworld.com/forum91/1373.htm or http://www.highdots.com/forums/javascript/calculate-height-iframe-problem-asp-53776.html – Martin Nycander Nov 01 '09 at 13:43
-
1@Mickey. It is not possible to find out the height of a third party webpage inside an iframe. See [my answer](http://stackoverflow.com/questions/4058217/get-height-of-iframe-with-external-url/4087397#4087397) to the [Get height of iframe with external URL](http://stackoverflow.com/q/4058217/445073) question to see why. – Day Feb 01 '11 at 21:44