Page 1 of 1

javascript page refresh... expert advice appreciated...

Posted: Mon Jul 20, 2009 10:20 pm
by scarface222
Hey guys I am using a Jquery ajax function to update a page section however every time the page refreshes I lose the spot where the scroll bar was and it returns to the top. Is there anyway to fix this based on my code? I do not know where else to turn I have tried everything. Any advice will do, here is my script.

Code: Select all

function updatebox(){
        //just for the fade effect
        messageList.hide();
        loading.fadeIn();
        //send the post to box.php
        var topic_id = inputid.attr("value");
        $.ajax({
            type: "POST", url: "shoutter/shoutbox.php", data: "action=update&id=" + id,
            complete: function(data){
                loading.fadeOut();
                messageList.html(data.responseText);
                messageList.fadeIn(2000);
            }
        });
    }
Then I tried this call however the page just keeps refreshing in a fashion that is useless since the scroll bar resets

Code: Select all

setInterval(updatebox, 3000);

Re: javascript page refresh... expert advice appreciated...

Posted: Tue Jul 21, 2009 11:34 am
by kaszu
As I understand page doesn't reload, it just hides the content, which goes over the fold. Here is the fix (with '+' are marked changes)

Code: Select all

function updatebox(){
        //just for the fade effect
        var scrollPosition = $(window).scrollTop();    //+ save scroll position
        messageList.hide();
        loading.fadeIn();
        //send the post to box.php
        var topic_id = inputid.attr("value");
        $.ajax({
            type: "POST", url: "shoutter/shoutbox.php", data: "action=update&id=" + id,
            complete: function(data){
                loading.fadeOut();
                messageList.html(data.responseText);
                messageList.fadeIn(2000);
                $(window).scrollTop(scrollPosition);    //+ scroll to previously saved position
            }
        });
    }

Re: javascript page refresh... expert advice appreciated...

Posted: Tue Jul 21, 2009 11:49 pm
by scarface222
Thanks alot really appreciate it.