mkoryak mkoryak - 1 year ago 257
jQuery Question

how to get scrollTop of an iframe

jQuery's scrollTop returns null when window is an iframe. Has anyone been able to figure out how to get scrollTop of an iframe?

more info:

my script is running in the iframe itself, the parent window is on another domain, so i cant access the ID of the iframe or anything like that

Answer Source

You can set scrollTop by using this setup:


So you could try getting it like this:


Because different browsers set the scrollTop on different elements (body or html).

From the scrollTo plugin:

But that will probably still fail in certain browsers. Here is the relevant section from the source code of Ariel Flesher's scrollTo plugin for jQuery:

// Hack, hack, hack :)
// Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
$.fn._scrollable = function(){
    var elem = this,
      isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;

    if( ! isWin ) {
      return elem;

    var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;

     return $.browser.safari || doc.compatMode == 'BackCompat' ?
       doc.body : 

You may then run:


To determine how far the iframe has scrolled down.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download