/    Sign up×
Community /Pin to ProfileBookmark

Maintain Scroll Position on every refresh page and on button click

I have one DIV tag, one textbox and button all are server controls,
in DIV tag i displayed data from DB, i want to keep this DIV tag with fresh content every time….

I set DIV style as OVERFLOW – AUTO, so when content over flow, scroll bar is appear….Now i want to maintain that scrollbar position…as set by end-user…
for example if user drag scroll bar at middle and page is refreshed or button clickevent fired ..the scrollbar remain at same position, whc set by end-user.

Plz. Help me to solve.
Thanks
Chirag

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JSep 26.2006 — You could try something like this maybe

[code=php]<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script type="text/javascript">
<!--
cookieName="divscrollpos"
expdays=365

// An adaptation of Dorcht's cookie functions.

function setCookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure")
}

function getCookie(name) {
var arg = name + "="
var alen = arg.length
var clen = document.cookie.length
var i = 0
while (i < clen) {
var j = i + alen
if (document.cookie.substring(i, j) == arg){
return getCookieVal(j)
}
i = document.cookie.indexOf(" ", i) + 1
if (i == 0) break
}
return null
}

function getCookieVal(offset){
var endstr = document.cookie.indexOf (";", offset)
if (endstr == -1)
endstr = document.cookie.length
return unescape(document.cookie.substring(offset, endstr))
}

function deleteCookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT"
}


function saveDivScrollPos(){
var expdate = new Date()
expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000))
posY = (document.getElementById("d1").scrollTop)
setCookie(cookieName,posY,expdate)
}

function loadDivScrollPos(){
divYPos=getCookie(cookieName)
if(!divYPos){
return
}
document.getElementById("d1").scrollTop=divYPos
}

// add onload="loadDivScrollPos()" onunload="saveDivScrollPos()" to the opening BODY tag

// -->
</script>

</HEAD>
<BODY onload="loadDivScrollPos()" onunload="saveDivScrollPos()">

<div id="d1" style="height:300px;overflow:auto">
Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>
Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>
Dummy Text<br><br><br><br>Dummy Text<br><br><br><br>
</div>

</BODY>
</HTML>[/code]
×

Success!

Help @webchirag spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...