首页 > 解决方案 > 浏览器后退按钮重定向脚本

问题描述

我正在尝试开发浏览器后退按钮重定向脚本。

所以基本上当用户到达谷歌或任何其他社交媒体的 index.html 时,点击后退按钮用户应该转到指定的页面。

我尝试使用 javascript 创建它,但它不起作用。有人可以帮忙吗?

我创建了 3 个页面

1) 索引.html

<!DOCTYPE html>
<html>
<body>

<p><a href="redirect.html">Redirect Page</a></p>

</body>
</html>

2)redirect.html

<!DOCTYPE html>
<html>
<head>
<script>
function init() {
setTimeout(function(){window.scrollTo(0,1)},0);
}

window.history.pushState(‘back.html’, ‘back’, ‘back.html’);
window.history.pushState(‘index.html’, ‘Index’, ‘index.html’);
window.addEventListener(“popstate”, function(e) {
if(document.URL.indexOf(“back.html”) >= 0){
document.location.href = document.location;
}

});
</script>
</head>

<body>

<p>By pressing the browser back button it should redirect to specified url</p>

</body>
</html>

3) back.html

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<script>

document.location.href = “https://www.google.com”;

</script>
</head>
<body>
</body>
</html>

标签: javascriptredirectbuttonbrowserback

解决方案


推荐阅读