【问题标题】:HTML5 History - Back button to previous full page?HTML5 历史 - 返回按钮到上一个完整页面?
【发布时间】:2016-05-13 19:25:55
【问题描述】:

我正在使用 HTML5 历史 API 修改 URL,因为选择了某些产品属性(例如绿色汽车、蓝色汽车)以允许深层链接共享。

但是,这不是单页应用程序,所以我不想劫持用户的后退按钮:如果他们按下后退按钮,我想让他们转到上一页,而不是之前的汽车颜色。

实现这一目标的最佳方法是什么?

历史示例:

/page1
/page2
/page2?color=green
/page2?color=red
/page2?color=blue

然后按浏览器的返回键回到/page1

【问题讨论】:

  • 完全不要使用后背。只需对最后一页执行前进操作。您可以使用 sessionStorage 或 localStorage 来存储最后访问的页面并检查每个页面中的更改。
  • 我喜欢这个想法,但就像我在问题中提到的那样,我希望人们能够使用浏览器的后退按钮(或 Android 的后退导航按钮)转到上一页。那是我可以通过 JS 接管的东西吗?
  • 不,你不应该这样做,即使你可以。您将破坏浏览器的后退按钮功能和用户期望。如果您想让他们选择转到上一页,那么您可以公开它,但您不能用这种潜在的恶意脚本破坏浏览器功能。
  • 正确,完全正确。所以不幸的是,你提出的解决方案对我不起作用。不过谢谢!
  • 您可以通过页面上的按钮/链接在浏览器中为您的应用程序提供此功能,使用类似于我上面建议的技术,但您不能中断按钮功能。

标签: javascript html html5-history


【解决方案1】:

看起来我应该一直在使用

history.replaceState();

而不是history.pushState();。它会替换浏览器的 URL,但不会添加到历史记录对象中,因此后退按钮可以按我的意愿工作。

history.replaceState() 的操作与 history.pushState() 完全相同,只是 replaceState() 修改当前历史条目而不是创建新条目。

developer.mozilla.org

【讨论】:

    【解决方案2】:

    这是一个使用 sessionStorage 的解决方案,它使您的应用程序能够转到以前存储的页面,而不会破坏可用性预期/浏览器后退按钮功能。移动到以前的 URL(例如不同的颜色)是用户使用浏览器上的后退按钮的预期结果。

    JS

    function getCurrentPath() {
        return sessionStorage.getItem("currentPath");
    }
    
    function getPreviousPath() {
        return sessionStorage.getItem("previousPath");
    }
    
    function setCurrentPath(path) {
        var currentPath = getCurrentPath();
        if (currentPath != path) {
            sessionStorage.setItem("previousPath", currentPath);
            sessionStorage.setItem("currentPath", path);
        } else {
            console.log('Path has not changed: ' + path);
        }
    }
    
    function goToPrevious() {
        var previousPath = getPreviousPath();
        if (previousPath && previousPath != 'null') {
            window.location.href = previousPath;
        } else {
            alert('Previous page is not defined.');
        }
    }
    

    test1.html

    <!DOCTYPE html>
    <html>
        <head>
            <title>Test 1</title>
            <meta charset="UTF-8">
            <script src="test.js"></script>
            <script>
                setCurrentPath('test1.html');
                console.log('Current Path:', getCurrentPath());
                console.log('Previous Path:', getPreviousPath());
            </script>
        </head>
        <body>
            <button onclick="goToPrevious();">Go to previous page</button>
            <a href="test2.html">Test 2</a>
            <a href="?color=red">Red</a>
            <a href="?color=green">Green</a>
            <a href="?color=blue">Blue</a>
        </body>
    </html>
    

    test2.html

    <!DOCTYPE html>
    <html>
        <head>
            <title>Test 2</title>
            <meta charset="UTF-8">
            <script src="test.js"></script>
            <script>
                setCurrentPath('test2.html');
                console.log('Current Path:', getCurrentPath());
                console.log('Previous Path:', getPreviousPath());
            </script>
        </head>
        <body>
            <button onclick="goToPrevious();">Go to previous page</button>
            <a href="test1.html">Test 1</a>
            <a href="?color=red">Red</a>
            <a href="?color=green">Green</a>
            <a href="?color=blue">Blue</a>
        </body>
    </html>
    

    您可以看到这允许用户更改查询字符串参数,但这不会影响您通过前进操作移动到上一页的功能,只需存储路径之间的更改即可。您可以通过使用数组来扩展它,而不是像我在这里所做的那样简单地使用两个值,但如果您愿意,我将把它留给实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2017-06-05
      • 2017-02-27
      • 2015-09-04
      • 2012-07-17
      • 2018-11-25
      • 1970-01-01
      相关资源
      最近更新 更多