【问题标题】:history.go() how to get the index of a specific visited page url from window.history?history.go() 如何从 window.history 获取特定访问页面 url 的索引?
【发布时间】:2013-03-26 07:48:44
【问题描述】:

如果单击将转到特定历史记录,我有一个具有按钮的 Web 应用程序。听说只有 IE 支持 history.go(url) 而其他浏览器只接受数值。在我的情况下,history.go() 只会转到特定页面,例如http://localhost:54187/Products/Create。即使用户在单击按钮时导航到不同的页面,它仍然会转到历史记录的“创建”页面。我需要history.go,因为它相当于浏览器来回。我不能使用 window.location 或 location.href,因为我的应用程序要求填写的数据仍然存在,这正是 history.go 所做的。如果用户总共加载了 26 个页面,我如何知道这 26 个页面中的哪一个是创建页面,以便我可以使用 history.go(createIndex)? window.history.length 给出 26 但它不公开 url。

请帮助我如何解决我的问题。

JavaScript 和 HTML

<script type="text/javascript">
    $(document).ready(function () {
    $('#btnGoToCreate').click(function () {
        var createIndex = //how to know what's the index of 
        //http://localhost:54187/Products/Create from window.history?
        history.go(createIndex);
        });
    });
   </script>

<input type="button" value="Go to Last Entry" id="btnGoToCreate" />

【问题讨论】:

  • “我不能使用 window.location 或 location.href,因为我的应用程序要求填充的数据仍然存在” - 这更多是缓存问题而不是 如何 页面被导航到。如果你需要用户之前输入的数据,你应该把它保存到你的会话中,这样你就可以从那里读取它。
  • @CBroe 很棒的建议

标签: javascript jquery asp.net-mvc-3


【解决方案1】:

由于安全/隐私原因,您无法阅读历史记录项。您只能访问以前的 URL,可以使用以下方式读取:

document.referrer

虽然您可以manipulate the browser history,但这也不允许您转到用户历史记录中的特定页面。

【讨论】:

    【解决方案2】:

    添加一个计数器变量来保存索引。这个计数器可以存储在cookie/sessionStorage/localStorage
    当用户导航到另一个页面时,counter self 加 1,最后你可以在用户单击按钮时使用 window.history.go(counter)。

    顺便说一句,当用户刷新页面时,计数器可以自己加1,为避免此问题发生,您需要将时间戳传递给下一页,使用?timestamp=1231232123,并保存在cookie/sessionStorage/localStorage,如果时间戳在url与cookie/sessionStorage/localStorage中保存的url不同,则计数器可以加1。

    这是我项目中的解决方案,希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2020-02-19
      相关资源
      最近更新 更多