az9214
asked on
problem while using Javascript to maintain scroll position after page reload in a dynamic(PHP) webpage.
Hi there,
I am trying to get a php page to maintain scroll position after the page has been reloaded.
The page reloading is done using javascript and the code i have implemented so far in order to maintain the scroll position is also written using javascript. Here are some snippets of the code
function productQty(prodid,cpath){
setCookie('divid',prodid);
}
function setCookie(c_name,value,exp iredays)
{
var exdate=new Date();
exdate.setDate(exdate.getD ate()+expi redays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTSt ring());
}
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.in dexOf(c_na me + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.inde xOf(";",c_ start);
if (c_end==-1) c_end=document.cookie.leng th;
return unescape(document.cookie.s ubstring(c _start,c_e nd));
}
}
return "";
}
window.onload = function(){
var divid=getCookie('divid');
var objDiv = document.getElementById(di vid);
objDiv.scrollTop = objDiv.scrollHeight;
}
and heres an example of a div that i am trying to get the page to scroll down to.
<div id="708"> <input type="text" style="width:40px;" name="order_product[708]" class="check_cp_input" value="" title="48" /> </div>
and this is where the javascript function store the div cookie is called
<div onclick="return productQty(708,16);" ><img src="images/buynow.jpg" border="0" style="cursor:pointer;"/> </div>
So far, the page does not scroll down to the supplied div on reload and i do not know what i am doing wrong. Any help into
understanding the problem will be greatly appreciated.
I am trying to get a php page to maintain scroll position after the page has been reloaded.
The page reloading is done using javascript and the code i have implemented so far in order to maintain the scroll position is also written using javascript. Here are some snippets of the code
function productQty(prodid,cpath){
setCookie('divid',prodid);
}
function setCookie(c_name,value,exp
{
var exdate=new Date();
exdate.setDate(exdate.getD
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTSt
}
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.in
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.inde
if (c_end==-1) c_end=document.cookie.leng
return unescape(document.cookie.s
}
}
return "";
}
window.onload = function(){
var divid=getCookie('divid');
var objDiv = document.getElementById(di
objDiv.scrollTop = objDiv.scrollHeight;
}
and heres an example of a div that i am trying to get the page to scroll down to.
<div id="708"> <input type="text" style="width:40px;" name="order_product[708]" class="check_cp_input" value="" title="48" /> </div>
and this is where the javascript function store the div cookie is called
<div onclick="return productQty(708,16);" ><img src="images/buynow.jpg" border="0" style="cursor:pointer;"/> </div>
So far, the page does not scroll down to the supplied div on reload and i do not know what i am doing wrong. Any help into
understanding the problem will be greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The solution works.