Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm using jquery and cookie.jquery to remember the scroll position. Is there anything I can do to improve my code? Thanks!

$(document).ready(function() {

    // convert cokkie variable for easy access
    var prev_scroll_position = $.cookie('prev_scroll_position');

    // scroll to position
    $(window).scrollTop(prev_scroll_position);

    // update cookie when user scrolls
    $(window).scroll(function (event) {
        var scroll_positon = $(window).scrollTop();
        $.cookie('prev_scroll_position', scroll_positon, { expires: 7, path: '/' });
    });

});
share|improve this question
    
You could use a debounce function, so the scroll function wont be called for every pixel, but after scrolling has stopped for Xms - davidwalsh.name/javascript-debounce-function. This will prevent the scroll-function to be a bottleneck while scrolling –  TryingToImprove Feb 2 at 13:19
    
I have created a example here: jsfiddle.net/6x8kkjhu –  TryingToImprove Feb 2 at 13:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.