Return to the previous page view location

1. If the previous page is static, may be history.go (-1) method;

  go () method to load a specific page in the history list.

  This parameter can be a number, using the relative position of the URL you want to access the list of URL History in. (-1 on a page, a page forward 1). Or a string, the string must be a partial or complete URL, the function will go to match the first URL string.

  This method returns the previous page, without refreshing the page, but will execute js;

example:

<a href="javascript:;" onclick="javascript:history.go(-1);"> Back </a>

click on the back page will be returned to the browser location on a page
2. I met at work , using VUE page, the page will be loaded each time to request data, (-1) return to the previous method with history.go, since the previous re-page request data, to the location of the page is not the last visit;

Solution: Use onbeforeunload (event triggered when about to leave the current page (refresh or closed)) method when the page is leaving the position of the scrollbar and the height of the page text into a cookie, remove the scroll bar position and when the page is refreshed or reloaded page text height, set to the page.

code show as below:

window.onbeforeunload = function () {
var scrollPos, height;
if (typeof window.pageYOffset != 'undefined') {
scrollPos = window.pageYOffset;
} else if (typeof document.compatMode != 'undefined' &&
document.compatMode != 'BackCompat') {
scrollPos = document.documentElement.scrollTop;
} else if (typeof document.body != 'undefined') {
scrollPos = document.body.scrollTop;

}
height = document.body.scrollHeight;
document.cookie = "scrollTop=" + scrollPos; //存储滚动条位置到cookies中
document.cookie = "height=" + height; //存储滚动条位置到cookies中
}
new Vue({
  el:'#app',
  
  Updated: function () { 
  var height;
  page height // get previous
  IF (! document.cookie.match (/ height = ([^;] +) (; | $) /) = null) {
  var Document ARR = .cookie.match (/ height = ([^ ;] +) (; | $) /); // cookies are not empty, the page height reading
  height = the parseInt (ARR [. 1]);
  }
  // Get before the position of the scrollbar
  IF (document.body.scrollHeight == height) {
   IF (document.cookie.match (/ scrollTop = ([^;] +) (; |! $) /) = null) {
  var ARR = document.cookie.match (/ scrollTop = ([^ ;] +) (; | $) /); // cookies are not empty, the read position of the slider
  document.documentElement.scrollTop = parseInt (arr [1] );
  document.body.scrollTop = the parseInt (ARR [. 1]);
  }
  }
  }

})


Guess you like

Origin www.cnblogs.com/yuanqt/p/11125913.html