【问题标题】:Redirecting back button to specific page using history API使用历史 API 将返回按钮重定向到特定页面
【发布时间】:2016-12-09 15:35:09
【问题描述】:

我有一个在线测试,它连续运行超过 5 个网页而没有暂停。我希望后退按钮(如果使用)重定向到测试页面之前的页面,防止它被用来循环遍历测试页面。我认为历史 API 可能是要走的路,并尝试将它与这个脚本一起使用:

var stateObj = { url: "/index.php/toeic_introduction"};

// Updates the current history entry   timerArray[i][2].
window.history.replaceState(stateObj, "","/index.php/toeic_introduction");

// Creates a new history entry.
window.history.pushState(stateObj, "","/index.php/toeic_introduction");

这适用于网址显示在地址行中但后退按钮未按要求重定向的情况。我什至不确定是否可以通过这种方式实现对后退按钮的控制。我曾想过禁用后退按钮并在网上四处查看,但这个选项似乎相当有问题。我当然欢迎任何 cmets 或建议。

【问题讨论】:

    标签: javascript html5-history


    【解决方案1】:

    如果添加带有 Javascript 历史记录的 pushtState,则应使用 window.onpopstate 来管理后退按钮操作。

    您将在此页面中找到所有需要的文档:

    https://developer.mozilla.org/fr/docs/Web/API/WindowEventHandlers/onpopstate

    【讨论】:

      【解决方案2】:

      我猜你需要绑定到popState 事件并将你的重定向逻辑放在那里:

         $(document).ready(function() {
            if (window.history && window.history.pushState) {
              $(window).on('popstate', function() {
                // your logic here
              });
            }
         });
      

      【讨论】:

      • 这不起作用。测试开始后地址行不显示“/index.php/toeic_introduction”。我已经检查了警报并且正在访问该功能。
      猜你喜欢
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多