So here's my trouble. I am trying to position some scroll bars on my page. These scrollbar positions are being persisted by Cookies. When the page loads I am not always able to read the cookies in time before firing my Position Scroll Bars Code.
I am using this code in a SharePoint Solution and have written the code using VS 2005 in codebehind.
Code:
function CreateCookie(name,value) {
document.cookie = name+"="+value+"; path=/";
}
function setScrollPos(posTop,posLeft){
//Persist scrollbars to cookies
var oImage = document.getElementById('ctl00_PlaceHolderMain_divGanttChartBody');
if (oImage && PageLoaded)
{
var oImageSize = document.getElementById('ctl00_PlaceHolderMain_ganttChart');
var percentLeft = 0;
if(oImageSize.width - parseInt(oImage.style.width) != 0)
{
percentLeft = (posLeft / (oImageSize.width - parseInt(oImage.style.width)) * 100);
}
CreateCookie('OffsetY', percentLeft);
document.getElementById('ctl00_PlaceHolderMain_divGanttChartHeader').scrollLeft = posLeft;
CreateCookie('OffsetX', posTop);
}
}
function SetCookieForSelectedItem()
{
CreateCookie('OffsetX', 280);
CreateCookie('OffsetY', 38.060715903942);
SetScrollBars();
}
SetCookieForSelectedItem();
function ReadCookie(name)
{
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
var PageLoaded = false;
function SetScrollBars()
{
var oImage = document.getElementById('ctl00_PlaceHolderMain_divGanttChartBody');
if (oImage)
{
var horizontalScrollPosition = parseFloat(ReadCookie('OffsetY'));
var oImageSize = document.getElementById('ctl00_PlaceHolderMain_ganttChart');
var horizScrollInt = (horizontalScrollPosition / 100) * (oImageSize.width - parseInt(oImage.style.width));
document.getElementById('ctl00_PlaceHolderMain_divGanttChartBody').scrollLeft = parseInt(horizScrollInt);
document.getElementById('ctl00_PlaceHolderMain_divGanttChartHeader').scrollLeft = parseInt(horizScrollInt);
var verticalScrollPosition = parseFloat(ReadCookie('OffsetX'));
document.getElementById('ctl00_PlaceHolderMain_divGanttChartBody').scrollTop = verticalScrollPosition;
PageLoaded = true;
}
}
SetScrollBars();
I am using this code in a SharePoint Solution and have written the code using VS 2005 in codebehind.