Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Race Condition

Status
Not open for further replies.

bam720

Technical User
Sep 29, 2005
289
US
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.

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.
 
When the page loads I am not always able to read the cookies in time before firing my Position Scroll Bars Code.

Can you clarify this? Your "ReadCookie" call is inside your "SetScrollBars" call, so you should be fine on that front. Even so, perhaps waiting until page-load before calling "SetScrollBars" might give you different behaviour?

Incidentally, does the "ReadCookie" call happen OK? If so, at what stage is your code failing?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top