location.replace(),并在返回数据时移动到新页面

location.replace() and move to a new page when data is returned

本文关键字:数据 移动 新页面 返回 replace location      更新时间:2023-09-26

$.post()返回一些数据后,我需要移动到另一个页面?

verifystudent.html:

$.post('students/verify.php',
    function(results){
        preventdefault();
        location.replace("verifystudent.html");
        window.location.href = "completed/student_result.html;
    }

基本上,当数据被接收到verifystudent.html时,它应该移动到student_result.html,并且当点击浏览器后退按钮时,它应避免转到verifystudent.html。我该怎么做?

试试这个脚本,它会阻止浏览器的后退按钮,所以在verifystudent.html 中使用它

<script type = "text/javascript" >
    history.pushState(null, null, 'pagename');
    window.addEventListener('popstate', function(event) {
         history.pushState(null, null, 'pagename');
    });
</script>

希望这能有所帮助。

使用SessionStorage保存应用程序的状态。您可以创建一个密钥来标记学生已验证。

加载验证页面时检查此密钥,如果学生已经验证,则使用location.replace更改URL。

另请阅读这篇关于防止用户转到上一页的文章。

对于您的第二个问题",当点击浏览器返回时,应避免转到verifystudent.html"

我在搜索同一个问题,在一个网站上发现了以下代码。想在这里分享一下:

function noBack()
{
   window.history.forward()
}
noBack();
window.onload = noBack;
window.onpageshow = function(evt){ if(evt.persisted) noBack(); }
window.onunload = function(){ void(0); }

然而,正如许多开发人员所指出的,这从来都不是一个好的做法,出于各种原因都应该避免。

您可以在verifystudent.html上使用JQuery navigation事件将用户发送到所需的页面

 $(window).on("navigate", function (event, data) {
      var direction = data.state.direction;
      if (direction == 'back') {
         window.location.href = "completed/student_result.html;
      }
}