JavaScript history

来源:互联网 发布:外汇手机交易软件 编辑:程序博客网 时间:2024/06/05 06:43

window.history 对象在编写时可不使用 window 这个前缀。

  • history.back()
    加载历史列表中的上一个 URL,与浏览器点击后退按钮作用相同。等价于history.go(-1)。
  • history.forward()
    加载历史列表中的下一个 URL,与浏览器点击向前按钮作用相同。等价于history.go(-1)。
  • history.go(int n)
    加载历史列表中向前数第n个的URL(n为负则向后,想到与back)

测试代码如下

<html>    <head>        <style type="text/css">            .widgetStyle {                width:100px;            }        </style>        <script>            function init() {                document.getElementById("number").value = -1;            }            function goHistory() {                var n = document.getElementById("number").value;                history.go(n);            }        </script>    </head>    <body onload="init()">        <button type="button" class="widgetStyle" onclick="javascript:history.forward()">forward</button>        <button type="button" class="widgetStyle" onclick="javascript:history.back()">back</button>        <div>            <input type="number" ime="inactive" class="widgetStyle" id="number"/>            <button type="button" class="widgetStyle" onclick="goHistory()">go</button>        </div>        <button type="button" class="widgetStyle" onclick="javascript:location='http://www.baidu.com'">relocate</button>    </body></html>
0 0