Get the current scrollTop value?

Discussion in 'HTML' started by Martin, Sep 18, 2012.

  1. Martin

    Martin Guest

    I have a page that contains an iframe. In the iframe is a table that
    contains a couple hundred rows of data. When the user clicks on a
    "Refresh" button on the page, I would like to capture the current
    scrollTop value of the table and pass it back to the server (where it
    will be returned to the refreshed page and a scrollTo will be done
    when the page has finished loading).

    I have the code functioning to do the actual scrolling when the page
    reloads - using dummy values like scrollTo(0,500) - but I can't figure
    out how to get the current position so it can be passed to the
    refreshed page.

    I've tried:
    document.getElementById("iframe_DL").contentDocument.scrollTop()
    and several variations but can't get anything to work.

    Can someone tell me how to do this?

    And before anyone asks:
    * No the page is not available for viewing. It's part of a user
    interface to a machine and is used only on a LAN.
    * Javascript will be functional on all users computers. It's already
    being used throughout this application.
    * Yes, the page in the iframe is served out by the same server that
    sends out the main page.
     
    Martin, Sep 18, 2012
    #1
    1. Advertising

  2. Martin

    Tim Streater Guest

    In article <>,
    Martin <> wrote:

    > I have a page that contains an iframe. In the iframe is a table that
    > contains a couple hundred rows of data. When the user clicks on a
    > "Refresh" button on the page, I would like to capture the current
    > scrollTop value of the table and pass it back to the server (where it
    > will be returned to the refreshed page and a scrollTo will be done
    > when the page has finished loading).
    >
    > I have the code functioning to do the actual scrolling when the page
    > reloads - using dummy values like scrollTo(0,500) - but I can't figure
    > out how to get the current position so it can be passed to the
    > refreshed page.


    I keep a note of the table's rownumber or whatever it is, and given the
    height of each row, which in my case is constant, I can compute the
    required scrollTo value.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Sep 18, 2012
    #2
    1. Advertising

  3. Martin

    Martin Guest

    On Tue, 18 Sep 2012 16:58:08 +0100, Tim Streater
    <> wrote:

    >In article <>,
    > Martin <> wrote:
    >
    >> I have a page that contains an iframe. In the iframe is a table that
    >> contains a couple hundred rows of data. When the user clicks on a
    >> "Refresh" button on the page, I would like to capture the current
    >> scrollTop value of the table and pass it back to the server (where it
    >> will be returned to the refreshed page and a scrollTo will be done
    >> when the page has finished loading).
    >>
    >> I have the code functioning to do the actual scrolling when the page
    >> reloads - using dummy values like scrollTo(0,500) - but I can't figure
    >> out how to get the current position so it can be passed to the
    >> refreshed page.

    >
    >I keep a note of the table's rownumber or whatever it is, and given the
    >height of each row, which in my case is constant, I can compute the
    >required scrollTo value.


    Thanks Tim - not a bad idea. But how do you know which row the user is
    looking at if all he did was use the iFrame's scroll bar to move the
    list?
     
    Martin, Sep 18, 2012
    #3
  4. Martin

    Tim Streater Guest

    In article <>,
    Martin <> wrote:

    > On Tue, 18 Sep 2012 16:58:08 +0100, Tim Streater
    > <> wrote:
    >
    > >In article <>,
    > > Martin <> wrote:
    > >
    > >> I have a page that contains an iframe. In the iframe is a table that
    > >> contains a couple hundred rows of data. When the user clicks on a
    > >> "Refresh" button on the page, I would like to capture the current
    > >> scrollTop value of the table and pass it back to the server (where it
    > >> will be returned to the refreshed page and a scrollTo will be done
    > >> when the page has finished loading).
    > >>
    > >> I have the code functioning to do the actual scrolling when the page
    > >> reloads - using dummy values like scrollTo(0,500) - but I can't figure
    > >> out how to get the current position so it can be passed to the
    > >> refreshed page.

    > >
    > >I keep a note of the table's rownumber or whatever it is, and given the
    > >height of each row, which in my case is constant, I can compute the
    > >required scrollTo value.

    >
    > Thanks Tim - not a bad idea. But how do you know which row the user is
    > looking at if all he did was use the iFrame's scroll bar to move the
    > list?


    Mmmm, yes actually you're right - sorry. In my case the user has clicked
    a row to select it and it's that row that I scroll to later.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Sep 18, 2012
    #4
  5. Martin

    BootNic Guest

    In article <>, Martin
    <> wrote:

    > I have a page that contains an iframe. In the iframe is a table that
    > contains a couple hundred rows of data. When the user clicks on a
    > "Refresh" button on the page, I would like to capture the current
    > scrollTop value of the table and pass it back to the server (where it
    > will be returned to the refreshed page and a scrollTo will be done when
    > the page has finished loading).


    > I have the code functioning to do the actual scrolling when the page
    > reloads - using dummy values like scrollTo(0,500) - but I can't figure
    > out how to get the current position so it can be passed to the
    > refreshed page.


    > I've tried:
    > document.getElementById("iframe_DL").contentDocument.scrollTop() and
    > several variations but can't get anything to work.


    > Can someone tell me how to do this?


    > And before anyone asks: * No the page is not available for viewing.
    > It's part of a user interface to a machine and is used only on a LAN. *
    > Javascript will be functional on all users computers. It's already
    > being used throughout this application. * Yes, the page in the iframe
    > is served out by the same server that sends out the main page.


    What browsers and versions? Makes a big difference on how one would go
    about trying to get the scrollTop or equivalent value.

    Matters not, change the way you reload the page. Consider using
    XMLHttpRequest and it just may be that scroll position will no longer be
    an issue.

    A possibility would also be to replace the iframe with a div and just
    use the innerHTML of the body of the page.

    With the following function an iframe could be reloaded, or with slight
    modification replacing the iframe with a div should be an easy thing to
    complete.

    function doIframe(id) {
    var myIframe = document.getElementById(id),
    ifrm,
    obj;
    if ('contentDocument' in myIframe) {
    ifrm = myIframe.contentDocument;
    obj = ifrm.body;
    } else if ('contentWindow' in myIframe) {
    ifrm = myIframe.contentWindow;
    obj = ifrm.document.body;
    } else {
    return;
    }
    var xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() :
    (window.ActiveXObject) ? new ActiveXObject('Msxml2.XMLHTTP') : null,
    url = ifrm.location;
    if (xmlhttp) {
    xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
    obj.innerHTML = xmlhttp.responseText.
    match(/<(html)\s*[^>]*>([\S\s]*?)<\/\1>/i)[2];
    }
    };
    xmlhttp.open("POST", url, true);
    xmlhttp.send(null);
    }
    }


    --
    BootNic Tue Sep 18, 2012 07:51 pm
    They always say time changes things, but you actually have to change them
    yourself.
    *Andy Warhol*

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.11 (GNU/Linux)

    iEYEARECAAYFAlBZCP8ACgkQOcdbyBqMFBHc8wCgnOWffntIGizbldEatbecOYa8
    SgkAoNjnO4Z8zic3aVCzZMPKE1XTgVhq
    =l8SF
    -----END PGP SIGNATURE-----
     
    BootNic, Sep 19, 2012
    #5
  6. Martin

    BootNic Guest

    In article <>,
    BootNic <> wrote:

    [snip]

    > function doIframe(id) {
    > var myIframe = document.getElementById(id),
    > ifrm,
    > obj;
    > if ('contentDocument' in myIframe) {
    > ifrm = myIframe.contentDocument;
    > obj = ifrm.body;
    > } else if ('contentWindow' in myIframe) {
    > ifrm = myIframe.contentWindow;
    > obj = ifrm.document.body;
    > } else {
    > return;
    > }
    > var xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() :
    > (window.ActiveXObject) ? new ActiveXObject('Msxml2.XMLHTTP') : null,
    > url = ifrm.location;
    > if (xmlhttp) {
    > xmlhttp.onreadystatechange = function () {
    > if (xmlhttp.readyState == 4) {


    > obj.innerHTML = xmlhttp.responseText.
    > match(/<(html)\s*[^>]*>([\S\s]*?)<\/\1>/i)[2];


    should be:

    obj.innerHTML = xmlhttp.responseText.
    match(/<(body)\s*[^>]*>([\S\s]*?)<\/\1>/i)[2];

    > }
    > };
    > xmlhttp.open("POST", url, true);
    > xmlhttp.send(null);
    > }
    > }



    --
    BootNic Tue Sep 18, 2012 08:09 pm
    Happiness is not something you experience, it's something you remember.
    *Oscar Levant*

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.11 (GNU/Linux)

    iEYEARECAAYFAlBZDVgACgkQOcdbyBqMFBHtDACgzbKFg/LCXda+LYoDMXn9o0Fm
    d4wAnib+bnjXLoYRjRMoEQ9MiGZXr6Vn
    =ehfL
    -----END PGP SIGNATURE-----
     
    BootNic, Sep 19, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    5,966
  2. =?Utf-8?B?QW5keQ==?=

    master pages and scrollTop = 0

    =?Utf-8?B?QW5keQ==?=, May 17, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    3,401
    Ken Cox [Microsoft MVP]
    May 17, 2006
  3. Replies:
    0
    Views:
    704
  4. Replies:
    0
    Views:
    1,169
  5. =?Utf-8?B?SGFyZHkgV2FuZw==?=

    HTML document.body.scrollTop question

    =?Utf-8?B?SGFyZHkgV2FuZw==?=, Feb 22, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,721
    bruce barker
    Feb 23, 2007
Loading...

Share This Page