0

I know that trying to scroll the iframe itself in not the way to go and instead i should be scrolling the div. Thing is it doesn't work. What is wrong with this code? Is it a google thing? I'm using the custom search so it shows up in the frame but I know google and frames don't like to play with each other.

HTML

<div id="googleframe"><iframe id="googleseo" src="http://www.google.com/custom?q=hey+there&btnG=Search"></iframe></div>

JS

var seoFrame = document.getElementById('googleseo');
seoFrame.src = googleSearch;
seoFrame.onload = function () {
    document.getElementById('googleframe').scrollTop = 300;
    }
}
SaintCad
  • 59
  • 1
  • 10
  • I think it does not work because of the same domain policy. There are workarounds [described in this SO question](http://stackoverflow.com/questions/1192228/scrolling-an-iframe-with-javascript#comment6499395_1229879). – A1rPun May 18 '15 at 14:38

2 Answers2

1

It was a CSS issue. The iframe wasn't long enough to scroll. It was the same height as my div so I made it longer and it works perfectly.

SaintCad
  • 59
  • 1
  • 10
0

This is due to the scrollTop referencing the iframe, not the body within the frame. The iframe itself doesn't have a scrollbar, it's the document within it.

Fiddle of it working http://jsfiddle.net/ebzxzgmo/

var seoFrame = document.getElementById('googleseo');
var elem = (seoFrame.contentDocument||seoFrame.contentWindow.document).documentElement;
elem.getElementsByTagName('body')[0].scrollTop = 300;

Notice that it's requesting jsfiddle. Browsers block accessing the DOM of iframes from other domains.

Reference for scrolling cross domain: Scroll a cross-domain child iframe?

Community
  • 1
  • 1
Devin H.
  • 1,065
  • 1
  • 11
  • 19
  • What is the double pipe operator? – SaintCad May 18 '15 at 14:50
  • That's an or operation. Depending on the browser, the iframe document is stored in a different place. So doing `seoFrame.contentDocument||seoFrame.contentWindow.document` says "if defined use .contentDocument OR use .contentWindow.document" – Devin H. May 18 '15 at 14:53