天天看点

HTML—js:利用History对象实现页面跳转

History常用的属性和方法:

length:返回浏览器历史列表中的URL数量

back():加载history列表中的前一个URL

forward():加载history列表中的下一个URL

go():加载history列表中的某个具体页面

代码:

<!DOCTYPE html>
<html>
<head >
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>第一个页面</h1>
    <a href="historyPage2.html" target="_blank" rel="external nofollow" >下一个页面</a>
    <input type="button" value="前进" οnclick="next()">
    <input type="button" value="最后一个页面" οnclick="goPage()">
</body>
<script>
    alert(history.length);

    function next(){
        history.forward();
    }
    function goPage(){
        history.go(3);
    }

</script>
</html>
           
<!DOCTYPE html>
<html>
<head >
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>第二个页面</h1>
<a href="historyPage3.html" target="_blank" rel="external nofollow" >下一个页面</a>
<input type="button" value="前进" οnclick="next()">
<input type="button" value="后退" οnclick="back()">
</body>
<script>
    alert(history.length);
    function next(){
        history.forward();
    }
    function back(){
        history.back();
    }
</script>
</html>
           
<!DOCTYPE html>
<html>
<head >
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>第三个页面</h1>
<a href="historyPage4.html" target="_blank" rel="external nofollow" >下一个页面</a>
<input type="button" value="前进" οnclick="next()">
<input type="button" value="后退" οnclick="back()">
<input type="button" value="返回到首页" οnclick="goPage()">
</body>
<script>
    alert(history.length);
    function next(){
        history.forward();
    }
    function back(){
        history.back();
    }
    function goPage(){
        history.go(-2);
    }
</script>
</html>
           
<!DOCTYPE html>
<html>
<head >
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>第四个页面</h1>
<input type="button" value="后退" οnclick="back()">
</body>
<script>
    alert(history.length);
    function back(){
        history.back();
    }
</script>
</html>
           

继续阅读