28
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

リロードさせた後、スクロール位置を復元

Last updated at Posted at 2017-08-22

IE6,IE11,Firefoxにて確認。

keep_scroll_reload() を呼んでGET値にx,y座標を渡し、
リロード後にスクロールする

// リロード TODO: ?が無い場合対応
function keep_scroll_reload() {
          var re = /&page_x=(\d+)&page_y=(\d+)/;
          var page_x = document.documentElement ? document.documentElement.scrollLeft : document.body.scrollLeft;
          var page_y = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;
          var position = '&page_x=' + page_x + '&page_y=' + page_y;
          if(!url.match(re)) {
                  //初回
                  location.href = url + position;
          } else {
                  //2回目以降
                  location.href = url.replace(/&page_x=(\d+)&page_y=(\d+)/,position);
          }
}

// スクロール位置を復元
function restore_scroll() {
        var re = /&page_x=(\d+)&page_y=(\d+)/;
        if(window.location.href.match(re)) {
                var position = window.location.href.match(re)
                window.scrollTo(position[1],position[2]);
        }
}

(window.onload = function() {
        restore_scroll();
})();
28
37
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?