0

so I have this javascript:

$(window).scroll(function () {
    if ($(window).scrollTop() > 0 && !document.contains(document.getElementById('toTop'))) {
        var top = '<div id="toTop" onclick="tTop()"></div>';
        $('body').append(top);
    }
});
function tTop() {
    $('html,body').scrollTop(0);
}
$(window).scroll(function () {
    if (document.contains(document.getElementById('toTop')) && $(window).scrollTop() == 0) {
        $('div').remove('#toTop');
    }
});

It works just fine if the page is loaded while the scroll bar is at the top; however when I refresh the page while the scroll bar is at the bottom, the page will still scroll up, but the scroll stays at the bottom. Can anyone tell me how to fix this?

3
  • 1
    You realise that every time that you scroll that top will be appended to your page...
    – Steven
    Commented Aug 9, 2013 at 13:52
  • Yeah, I fixed that literally seconds after I asked this hahaha, I'll update the code now. Commented Aug 9, 2013 at 14:36
  • Did you manage to get this working in the end?
    – Steven
    Commented Aug 10, 2013 at 13:19

1 Answer 1

1

when I refresh the page while the scroll bar is at the bottom, the page will still scroll up, but the scroll stays at the bottom

Maybe I'm misreading this, but are you concerned that the buttons are staying at the bottom of the page? Or $(window).scrollTop() is set to 0 after you click the button, after a page refresh?

Regardless, I would separate the scroll callback to another method, checking if the element exists (I used a button for an example instead of a div)

function scrollCallback() {
    if ($(window).scrollTop() > 0) {
        if ($('body').has('#toTop').length == false) {
            var top = '<input type="button" id="toTop" onclick="tTop();" value="top" />';
            $('body').append(top);
        }
    } 
    else {
        // Removes the button if the scroll is at the top of the page.
        $('body #toTop').remove();
    }
}

Then on your page load, call the scrollCallback, and set your scroll to also use the scroll back:

$(function() {
    // Execute it when you load the page.
    scrollCallback();       
    $(window).scroll(scrollCallback);
});
7
  • I'm not sure what the second half of your answers is saying but that sounds more like what I'm looking for. However, I have no problem removing the button, I was just about to update the code anyway, I'll be sure to try the second half of this though. Thank you. Commented Aug 9, 2013 at 14:35
  • The first code block adds a check to see if the button exists on the web page. It also removes it with the same function/binding. I noticed that you're binding $(window).scroll( twice which could be part of the issue. The second/last code block runs the function once after the page is loaded, checking where the scroll position if anything needs to be done (adding or removing the button), then it binds the scroll event to the call back (which you were already doing).
    – matth
    Commented Aug 9, 2013 at 14:40
  • Well, I can add and remove the button just fine now, but it's just the scroll bar won't go up with the page, and this only happens if I refresh the page with the scroll bar already at the bottom. Commented Aug 9, 2013 at 14:42
  • I'm able to use my code in IE(10)/Chrome(28)/Firefox(22). The code you have in the question works in chrome/firefox (after refresh too), but doesn't at all in IE (document.contains doesn't work). Is your javascript in a document ready function (Ex: $(function() {)?
    – matth
    Commented Aug 9, 2013 at 15:52
  • It is not in a document ready function, would that fix it you think? Commented Aug 9, 2013 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.