history.go()如何从window.history获取特定访问页面URL的索引?

时间:2013-03-26 07:48:45

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

我有一个带有按钮的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" />

2 个答案:

答案 0 :(得分:0)

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

document.referrer

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

答案 1 :(得分:0)

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

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

这是我项目中的解决方案,希望能帮到你。