Re: Retaining scroll position after asynchronous refresh

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 6:31 AM -0600 7/23/10, Larry Martell wrote:
Hello all-

I have a php script that generates a web page with a bunch of
scrollable frames, and then asynchronously refreshes the page. If the
user has scrolled down in any of the frames, when the refresh occurs
it has scrolled back to the top of all the frames. Is there a way I
can retain the scroll position so after the refresh the frames are
shown at the same location as before the refresh? I have googled and
googled for this, but everything I find is ASP or C# or Java. My stuff
is straight php/html. How can I do this with that?

TIA!
-larry


I forgot, this *is* Friday (as Daniel reminded me) -- so everyone here's the code I provided to Larry.


-larry:

This is a problem that can be solved via javascript. The process is that you create a cookie containing the scroll position value before the operation (whatever that may be) and then you restore the page back to where it was after the operation.

The following is the javascript code I wrote several years ago -- it works for me.

Cheers,

tedd

--- javascript ---

addLoadListener(init);

function init()
{
window.onscroll = function()
{
var scrollpos = getScrollingPosition();
document.title = 'left=' + scrollpos[0] + ' top=' + scrollpos[1];

//-- create a cookie named scrollPosition and put the vertical scroll amount in the value
var cookieName = "scrollPosition";
var cookieValue = scrollpos[1];
var theCookie = cookieName + "=" + cookieValue;
document.cookie = theCookie;
};

return true;
}

function getScrollingPosition()
{
var position = [0, 0];

if (typeof window.pageYOffset != 'undefined')
{
position = [
window.pageXOffset,
window.pageYOffset
];
}

else if (typeof document.documentElement.scrollTop != 'undefined'
&& (document.documentElement.scrollTop > 0 ||
document.documentElement.scrollLeft > 0))
{
position = [
document.documentElement.scrollLeft,
document.documentElement.scrollTop
];
}

else if (typeof document.body.scrollTop != 'undefined')
{
position = [
document.body.scrollLeft,
document.body.scrollTop
];
}

return position;
}

function addLoadListener(fn)
{
if (typeof window.addEventListener != 'undefined')
{
window.addEventListener('load', fn, false);
}
else if (typeof document.addEventListener != 'undefined')
{
document.addEventListener('load', fn, false);
}
else if (typeof window.attachEvent != 'undefined')
{
window.attachEvent('onload', fn);
}
else
{
var oldfn = window.onload;
if (typeof window.onload != 'function')
{
window.onload = fn;
}
else
{
window.onload = function()
{
oldfn();
fn();
};
}
}
}

function getCookie(searchName)
{
var cookies = document.cookie.split(";");

for (var i = 0; i < cookies.length; i++)
{
var cookieCrumbs = cookies[i].split("=");
var cookieName = cookieCrumbs[0];
var cookieValue = cookieCrumbs[1];

if (cookieName == searchName)
{
return cookieValue;
}
}
return false;
}

function myscroll()
{
// on load the cookie scrollPosition's value is read and scrolled to
var scrollValue = getCookie("scrollPosition");
window.scrollTo(0, scrollValue);
}



--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux