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

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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);
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

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

Post 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
            }
        });
    }
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post by scarface222 »

Thanks alot really appreciate it.
Post Reply