<!-- 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>
- 把代码写入 test.html
- 用 static-server 等工具启动http服务
- 打开 http://localhost:3000/test.html?jump=1
- 预期会从A页面自动跳转B页面,B页面点击返回能返回A页面
实际:iOS符合预期,安卓直接关闭了webview
Android上的webview 中,location.href 的改变不会被视为历史记录的一部分,因此点击返回时,webview 会直接关闭。可以用 window.history.pushState 来替代。
另外这里是微信开放社区,你这个是纯h5网页开发,可以去搜索引擎找寻答案。