0

I have done this Ajax jQuery function that saves the scroll position in a localStorage and it works fine on Chrome but it does not work on the other web browsers, what can I do to make it work on all platforms?

JS:

<script type="text/javascript">
            $.ajax({
            type: "GET",
            url: "AdminListUsers.aspx?column=Disabled&direc=False&a=chstat&z=+",
            success: function loadDoc() {
                $(window).unload(function () {
                    var scrollPosition = $("body").scrollTop();
                    localStorage.setItem("scrollPosition", scrollPosition);
                });
                if (localStorage.scrollPosition) {
                    $("body").scrollTop(localStorage.getItem("scrollPosition"));
                }
            }
        });
    </script>

C#:

var toggleUrl = "AdminListUsers.aspx?column=" + (IsClicked.FirstOrDefault().Key ?? "Name") + "&direc=" + (IsClicked.FirstOrDefault().Value) + "&a=chstat&q=" + id.ToString() + "&d=" + disabled + "&z=" + Server.UrlEncode(txtSearchFor.Text);

        var hl = new HyperLink();
        hl.Text = status;
        hl.Style.Add(HtmlTextWriterStyle.Color, (disabled ? "red" : "green"));
        hl.NavigateUrl = toggleUrl;
        hl.Attributes.Add("onclick", "loadDoc();return true;");
        cell.Controls.Add(hl);
        tr.Cells.Add(cell);
  • Which browsers does it not work in? It seems fairly standard. I'm guessing IE is the issue – Rory McCrossan Jun 15 '17 at 09:14
  • @RoryMcCrossan Not really, it only works on Edge and Chrome. Firefox, Safari and IE is the issue. – andrekordasti Jun 15 '17 at 09:15
  • [`scrollTop(n)`](https://api.jquery.com/scrollTop/#scrollTop2) is supposed to take a `Number`, but when you get the value from `localStorage` it's a string. I'm not sure that's the problem, because `$(body).scrollTop(42)` does not work for me either in Firefox... Hmmm strange – qlown Jun 15 '17 at 09:39
  • Oh, try with `$(window).scrollTop(n)` instead of `$(body)`, [see this](https://stackoverflow.com/questions/17776544/jquery-scrolltop-firefox-not-working) – qlown Jun 15 '17 at 09:41
  • @qlown Good job you were correct :) – andrekordasti Jun 15 '17 at 09:43

1 Answers1

0

The answer is to change the $("body").scrollTop(); to $(window).scrollTop();