假设有人来到我的某个页面。我想如果访问者点击浏览器后退按钮,而是转到上一页,它会将它们重定向到反馈页面,询问退出原因。现在我使用以下方式,这是正确的还是有更好的方法呢?
<script type="text/javascript">
window.onpopstate = function (event) {
if (typeof event.state !== 'undefined' && typeof event.state.page_name !== 'undefined' && event.state.page_name == 'third_page') {
window.location = "html3.html";
}
};
history.pushState({page_name: 'third_page'}, "page 3", "html3.html");
history.pushState({page_name: 'second_page'}, "page 2", "html2.html");
</script>