Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Leaving jQuery aside for a moment, using plain JavaScript you can check the following properties of <code>window</code> for the current vertical scrolling position:</p> <pre><code>window.pageYOffset; window.scrollY; </code></pre> <p>The second one is not cross-browser <a href="https://developer.mozilla.org/en-US/docs/DOM/window.scrollY" rel="nofollow">according to MDN</a>. Still according to that, on IE&lt;9 you must check <code>document.body.scrollTop</code>, as no property of <code>window</code> will give you the current scroll position. Actually, <code>document.body.scrollTop</code> is what I use most frequently, as in my experience it just works cross-browser (citation and checking needed here).</p> <p>jQuery takes the browser differences into consideration, and grabs the right property from the right object regardless of which selector you use for <code>.scrollTop()</code> (take a look at the <a href="https://github.com/jquery/jquery/blob/master/src/offset.js#l136-161" rel="nofollow">source code</a>). In short, it doesn't matter if you select <code>window</code> or <code>document</code> with jQuery when asking for <code>.scrollTop()</code>. It will look for <code>window.pageYOffset</code>, and if it can't find it, it will return <code>document.body.scrollTop</code>.</p> <hr> <p>A note about your comment on multiple documents per window: technically, each <code>window</code> object only points to a single <code>document</code>, and vice-versa. When you have multiple frames, there are separate <code>window</code> and <code>document</code> elements for each frame. For example, if you get a reference to a single iframe, you can get its own <code>window</code> and <code>document</code> from it:</p> <pre><code>// first iframe in main document var ifr = document.getElementsByTagName('iframe')[0]; var ifrWindow = ifr.contentWindow; var ifrDocument = ifrWindow.document; // The above is good for new and old browsers; // ifr.contentDocument is also available on modern browsers </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload