收藏
回答

如何解决从A页面自动跳转B页面,并且用户点击返回按钮回到A页面?

<!-- test.html -->
<!doctype html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8" />
    <title>test</title>
  </head>
  <body id="body">
    <h2 id="demo"></h2>
  </body>
  <script>
    var body = document.getElementById("demo");
    if (location.href.indexOf("jump") != -1) {
      body.innerHTML = "A: 主页面\n预期从这个页面自动跳转到B页面";
      setTimeout(() => {
        location.href = "/test.html";
      }, 2000);
    } else {
      body.innerHTML = "B: 二级页面\n预期点击返回,回到A页面";
    }
  </script>
</html>


  1. 把代码写入 test.html
  2. 用 static-server 等工具启动http服务
  3. 打开 http://localhost:3000/test.html?jump=1
  4. 预期会从A页面自动跳转B页面,B页面点击返回能返回A页面

实际:iOS符合预期,安卓直接关闭了webview

回答关注问题邀请回答
收藏

1 个回答

  • ZiraLi
    ZiraLi
    06-14

    Android上的webview 中,location.href 的改变不会被视为历史记录的一部分,因此点击返回时,webview 会直接关闭。可以用 window.history.pushState 来替代。

    另外这里是微信开放社区,你这个是纯h5网页开发,可以去搜索引擎找寻答案。

    06-14
    有用 1
    回复
登录 后发表内容