Wednesday, March 23, 2011

Maintaining Page Scroll Position Using Cookies

Some days ago I had come across with an issue regarding browser scroll position. Although the problem was a little trickier as the page was

including some JQuery expanders as well, but for now I’m just going to tell you how I managed to solve this problem.

As an ASP.NET programmer, I had a some very nice straight options:

  • I can use maintainScrollPositionOnPostBack="true" in web.config pages section to maintain scroll position for all pages.
  • I can use MaintainScrollPositionOnPostback="true" in page declaration for some specific page.
  • Programmatically set System.Web.UI.Page.MaintainScrollPositionOnPostBack = true; 

But I prefer to write this post for JQuery to make it available for other web technologies as well. So let’s start then :).

The solution can be broken down into two simple tasks:

  1. Save scroll position whenever user scrolls the page in a container like cookies.
  2. Set the scroll position when page is loaded again by reading the same container.

We can determine the user scroll action by simply registering the scroll event of the javascript window object when document ready event is fired i.e.

$(document).ready(function(){
$(window).scroll(SaveScrollPosition);
});

Here is the definition for SaveScrollPosition function:

function SaveScrollPosition()
{
var target = 'scrollTop';
var container = $(window);
$.cookie(target, container.scrollTop, { expires: 2 });
}

Here I’m planning to maintain only the vertical scroll position that’s why I’ve stored only scrollTop property of the container. For cookie management I’ve used the JQuery cookie plug-in written by Klaus Hartl.


Now let’s come to the second task of setting the scroll position upon page load. We can use the same document ready event written earlier for this task as follows:

$(document).ready(function(){
$(window).scroll(SaveScrollPosition);
var target = 'scrollTop';
var container = $(window);
if(typeof $.cookie(target) != 'undefined')
container.scrollTop = $.cookie(target);
});

I’ve used the same cookie name 'scrollTop' as used in SaveScrollPosition to retrieve cookie value and have set the scrollTop property of the container with this value.


Simple! Isn’t it???


Blogger Labels: Web,ASP.NET,Scroll,Cookies,JQuery


Technorati Tags: ,,,,

Thursday, March 10, 2011

Same bookmarks on all browsers – Cool Xmarks

Have you ever been in trouble to make same bookmarks on all browsers?

Have you ever done the following like me?

  1. Exporting bookmarks from one browser in a compatible format (HTML etc)
  2. Importing that compatible format into another browser
  3. Removing any duplicated bookmarks …
  4. Repeating step 1 to 3 for other browsers

Some browsers even have made this tedious process easier by directly providing a way to import bookmarks from other browsers on that computer. Even then it is quite a hassle to synchronize bookmarks on all the browsers on a computer.

Recently I’ve come up with a nice tool to do all the bookmarks synchronization automatically for me at the cost of only two clicks.

Xmarks

Yes it’s http://www.xmarks.com. With Xmarks you can not only keep the backup of your bookmarks on Xmarks server but can also sync across different browsers too including Firefox, Chrome, Internet Explorer and Safari (Mac OS). Moreover it provides a smarter search feature which highlights the top sites in search results based on how other users have bookmarked and rated them.

All you have to do is to download and install Xmarks tiny application on your computer in use and create a free account on http://www.xmarks.com.

Wednesday, February 11, 2009

Sharepoint 2007 SSO

Microsoft Sharepoint 2007 Single Sign On service provides an excellent platform to integrate different enterprise applications. Whether its a CRM system, an HR database or any other enterprise application, you can use credential store to log into these outside applications and can access all the required information to be displayed.