Get the current scrollTop value?

M

Martin

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.
 
T

Tim Streater

Martin said:
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.
 
M

Martin

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?
 
T

Tim Streater

Martin said:
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.
 
B

BootNic

Martin said:
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-----
 
B

BootNic

[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(/ said:
}
};
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-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top