Jason Gugg
Jason Gugg

Reputation: 35

save div scroll position on new page using cookie

I have a site that has a scrolling navigation bar.

I need to save the position of the scroll bar in a cookie and set it when the page is loaded.

i have tested it using body onload and on unload but it does not seem to be working.

Any help would be appreciated.

sample code is as follows.

<div class="ch-menu">
<ul id="ch-menu2">
  <?php 
    Some php code here to determine each link.

        if ($same != 0) 
        {
        ?>
            <li><a href="<?php $_SERVER['HTTP_HOST']?>/chunky/others/?catagory=<?php echo $link?>" title="View all Deals from <?php echo $text ?>"><?php echo $text?><span class="cat_count"><span style="top:-1px; position:relative;">(</span><?php echo $count ?><span style="top:-1px; position:relative;">)</span></span></a></li>
        <?php 
        } 
        else
        {?>
            <li class="ch_selected"><a href="<?php $_SERVER['HTTP_HOST']?>/chunky/others/?catagory=<?php echo $link ?>" title="View all Deals from <?php echo $text ?>"><?php echo $text?><span class="cat_count"><span style="top:-1px; position:relative;">(</span><?php echo $count ?><span style="top:-1px; position:relative;">)</span></span></a></li>
        <?php
        }
    } ?>
</ul>

Upvotes: 1

Views: 1230

Answers (1)

matthiasmullie
matthiasmullie

Reputation: 2083

This shouldn't actually be in the PHP category; all of it can be accomplished in JS. I assume you want to save the current scroll position when a user leaves the page? On the body unload event, create a cookie (through JS) that saves the value of document.body.scrollTop On body load, you then load the saved value from the cookie (through JS) and assign that value to scrollTop

Sources:

Upvotes: 1

Related Questions