How to make browser back and forward work on a single page layout?

痞子三分冷 提交于 2019-11-30 10:15:43
Christofer Eliasson

You can use the history API in HTML5 to achieve that.

Here are a few resources to get you started:

To get support in older browsers as well, there are JavaScript libraries that enable that:

Advanced example by the Chrome team, that uses the history API:

Yes, you can use HTML5 history API to implement it.

In older way, you can try to use bookmark, e.g.

    <html>
    <head>
        <script type="text/javascript">
            function goPage (v) {
                var idisplay = v == 'i',
                    adisplay = v == 'a',
                    bdisplay = v == 'b';
                document.getElementById('anchor_i').style.display = idisplay? 'none' : 'block';
                document.getElementById('anchor_a').style.display = adisplay? 'none' : 'block';
                document.getElementById('anchor_b').style.display = bdisplay? 'none' : 'block';

                document.getElementById('content_i').style.display = idisplay? 'block' : 'none';
                document.getElementById('content_a').style.display = adisplay? 'block' : 'none';
                document.getElementById('content_b').style.display = bdisplay? 'block' : 'none';
            }
        </script>
    </head>
    <body>
        <a name="index"></a>
        <div id="content_i">
            index
        </div>

        <a id="anchor_i" href="#index" onclick="goPage('i');" style="display: none">to index</a>
        <a id="anchor_a" href="#page_a" onclick="goPage('a');">to page_a</a>
        <a id="anchor_b" href="#page_b" onclick="goPage('b');">to page_b</a>

        <a name="page_a"></a>
        <div id="content_a" style="display: none">
            page a
        </div>
        <a name="page_b"></a>
        <div id="content_b" style="display: none;">
            page b
        </div>
    </body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!