javascript - 浏览器后退按钮重定向脚本
问题描述
我正在尝试开发浏览器后退按钮重定向脚本。
所以基本上当用户到达谷歌或任何其他社交媒体的 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”>
<head>
<script>
document.location.href = “https://www.google.com”;
</script>
</head>
<body>
</body>
</html>
解决方案
推荐阅读
- c++ - tsan 对 notify_all_at_thread_exit 的投诉
- xcode - 未找到 CloudFirestorePlugin 文件
- python - 如何安装 requirements.txt
- python - 阻止 Tweepy 脚本回复相同的提及
- swift - 我们如何在同一个控制器上使用 2 个 UIPicker?
- elasticsearch - Elasticsearch 嵌套数据搜索性能
- reactjs - 平面列表参考未定义反应原生
- javascript - 捕获 HTML5 流并提取音频以进行回放
- javascript - LocalStorage 在 WordPress 中不起作用
- python - seaborn stripplot 共享 x 轴