javascript - How does js enable the browser to return to the previous layer, so that we can return to the state when we last left it!
阿神
阿神 2017-05-16 13:08:10
0
5
537

If you want to return the page to the previous level, you can return to the state you got when you last left it, including a series of styles. There is no btn to return to the previous level on the page. It feels difficult to achieve! People on the Internet say to use hash, cookie, and storage. I feel that this is not reliable. I can't get the page to jump to or return to. Please give me some guidance!

阿神
阿神

闭关修行中......

reply all(5)
大家讲道理

window.history.go(-1);

伊谢尔伦

Simple, when you leave, add a parameter to your url: the address of the previous page after encode_url!

曾经蜡笔没有小新

Yes, there is currently no way to determine how the page is entered. It may be going back or forward.

左手右手慢动作

http://www.zhangxinxu.com/wor...

过去多啦不再A梦

Now I have 2 pages: Page A and Page B

Page B

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <button type="" onclick='back()'>返回A页</button>
  <script>
  function back() {
    window.history.go(-1);
  }
  sessionStorage.setItem('info', 'true'); //值必须为字符串
  </script>
</body>

</html>

Page A

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <a href="B.html">跳转到B页面</a>
  <script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
  <script>
  var flag = sessionStorage.getItem('info'); // IE不支持本地文件
  if (flag === 'true') {
    $('<span class="back-succeed">返回成功</span>').appendTo('body'); //如果是从其他页面返回本页,添加提示
    console.log('页面是从B页返回的');
    sessionStorage.removeItem('info');
  } else {
    console.log('页面是新进入的');
  }
  </script>
</body>

</html>

Process:

1. Enter page B from page A
2. On page B, we set up a sessionStorage with info===='true'
3. Page B calls the function back() through the button to execute window.history.go(-1) Returned to page A
4. After returning to page A, we obtain info and assign it to flag. If flag===='true' is determined, it means that it is returned from page B, and the value of info is cleared. If refreshed on page A, then The info has been cleared and will output: The page is newly entered.
5. If you open page A directly from the beginning and do not enter page B at this time, the info does not exist, and it will output: The page is newly entered

Copy the contents of pages A and B to test yourself - -

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template