Hello guys,

can anyone offer guidance on how to implementthe following:

I have a JSP page that automatically refreshes every minute. However the page is fairly long and when the user moves the scrollbar down the page, and the minute is
up the JSP page is refreshed and the user is automatically taken back to the start of the page, not where they where before.

I've found many examples whilst googl'n the internet but none which have really worked.

I think I just need to get the X and Y co-ordinates of the vertical scroll bar... store them...refresh the page and load them back in

Not sure if this function would be any use when loading them back in:

window.scrollTo(X,Y);

Thankyou

You'll have to get the scroll offsets and sent it to the server with every refresh, or you can store it in a cookie.

Then when the refreshed window has loaded, scroll the window to the saved scroll offsets.

Heres how to get the scroll offsets:

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

ref: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

To save stuff in cookies via javascript you can use;

/**
* Get a cookie value
* @param string cookie name
*/
commonLib.prototype.getCookie = function( 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 ) );
}

/**
* Set a cookie
* @param string cookie name
* @param string cookie value
* @param string cookie expiration counter in days
* @param string cookie path
* @param string cookie domain
* @param bool secure?
*/
commonLib.prototype.setCookie = function( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

/**
* Remove a cookie
* @param string cookie name
* @param string cookie value
* @param string cookie path
* @param string cookie domain
*/
commonLib.prototype.deleteCookie = function( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

ref: http://www.dustindiaz.com/top-ten-javascript/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.