I have a page which uses <meta http-equiv="refresh" content="240">
The problem im having is if i am half way downthe page when it refreshes, the refresh takes me back to the top of the page is it possible for the browser to keep the scrollbar position on refresh so that i do not loose my position on the page??
<SCRIPT LANGUAGE="JavaScript">
<!--
function getCookie(name)
{
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
function doload()
{
var scrollTop = getCookie ("scrollTop");
var scrollLeft = getCookie("scrollLeft");
if (!isNaN(scrollTop))
{
document.body.scrollTop = scrollTop;
document.documentElement.scrollTop = scrollTop;
I have been searching for this off and on for a couple of years and all I could find were wrong answers, code that didn't work and long pages of complicated code just to remember the scroll position. It can't possibly be that difficult.
Well, it isn't. It is super easy. I had to search a bunch of forums then whittle down each example until I finally got it to work with a minimum of fluff. You don't need a zillion lines of code, just two javascript snippets and a PHP cookie getter. You should be able to do it with pure javascript but I didn't need to so I mixed in some PHP.
This saves your current scroll position in a cookie whenever you scroll. Whenever the page loads it gets the scroll position from the cookie and loads the page at that scroll position. Voila! No more jumping to the top of the page when you reload. It works for links as well as the http-equiv refresh. I tried it with FireFox 15.0.1 and Chrome.
Well, it isn't. It is super easy. I had to search a bunch of forums then whittle down each example until I finally got it to work with a minimum of fluff. You don't need a zillion lines of code, just two javascript snippets and a PHP cookie getter. You should be able to do it with pure javascript but I didn't need to so I mixed in some PHP.
Fine if you have a server side language available, and you can be certain that nothing will overwrite your onscroll handler, and you'll never need to override the action when you link to your page using a #parameter or a querystring.
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
Terrible mistake on my part. I cut and pasted this from my PHP code. I tried to remove all the extras but missed some backslashes. Here is the fixed code.
Originally Posted by rsduhamel
A little more work and here it is with no PHP. Pure javascript.
First you need a read-cookie function. Put this in the <head> section of your page:
<script language="JavaScript">
function readCookie(name){
return(document.cookie.match('(^|; )'+name+'=([^;]*)')||0)[2]
}
</script>
Wonderful beat ! I would like to apprentice at the same time as you amend your site, how could i subscribe for a wephorum website? The account helped me a appropriate deal. I had been a little bit familiar of this your broadcast provided shiny transparent concept best wishes
My experience was much the same as yours until I found your solution. I find it to work beautifully in all major browsers except, of course, IE. Would you kindly supply the code for an IE workaround. Thanks.
Bookmarks