JS實現不重載頁面前提下建立一條歷史紀錄

背景

最近在上班過程當中,遇到了這麼一個需求,在多頁面應用中,須要在幾個頁面上共用同一個數據來源,且切換頁面不刷新頁面數據,並能實現歷史記錄的後退功能;因前期只考慮在一個頁面內實現多個頁面的效果,並未考慮到歷史記錄堆棧中的處理,致使頁面會一次性推出入口,如下爲總結的幾種解決方法。html

hash

在URL中,#咱們稱爲位置標識符,表明網頁的一個位置,在咱們剛開始接觸到a標籤的時候,咱們不少人都有操做過錨點跳轉,主要就是經過在 href 中設置想要跳到的位置的id值,在這個過程當中,頁面是沒有刷新的,但歷史記錄卻新增了一條;咱們利用window.location.hash能夠取得當前頁面的hash值,同時也能夠也能夠經過其寫入新的hash值,並經過監聽hashchange事件,來檢測hash值是否發生了改變。當咱們再點開彈框式的遮罩頁面的時候,能夠手動的去修改location.hash的值,這樣點擊window.history.back(),就能夠實現歷史記錄回退;html5

例子

代碼以下:api

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            background: #ccc;
        }
        .colorBlock {
            border: 10px solid #fff;
            height: 40vh;
            width: 40vh;
            margin: 20vh auto 10vh;
            color: #ffffff;
            font-size: 40px;
            line-height: 40vh;
            text-align: center;
        }
        .colorBlue{
            border: 10px solid #fff;
            height: 40vh;
            width: 40vh;
            margin: 20vh auto 10vh;
            color: #ffffff;
            font-size: 40px;
            line-height: 40vh;
            text-align: center;
            background: cornflowerblue;
        }
        .colorgray{
            border: 10px solid #fff;
            height: 40vh;
            width: 40vh;
            margin: 20vh auto 10vh;
            color: #ffffff;
            font-size: 40px;
            line-height: 40vh;
            text-align: center;
            background: lightcoral;
        }
        .colorgreen{
            border: 10px solid #fff;
            height: 40vh;
            width: 40vh;
            margin: 20vh auto 10vh;
            color: #ffffff;
            font-size: 40px;
            line-height: 40vh;
            text-align: center;
            background: greenyellow;
        }
        .btnBlock{
            text-align: center;
        }
        .btn{
            border: 5px solid #ffffff;
            font-size: 24px;
            line-height: 50px;
            width: 40vh;
        }
    </style>
</head>
<body>
<div id="content" class="colorBlue">
    加載中....
</div>
<div class="btnBlock">
    <button class="btn">change-url</button>
</div>
<script>
    (
        function () {
            var a=0;
            setInterval(function () {
                a++;
                document.getElementById("content").innerText=a;
            },1000)
        }
    )()
    window.addEventListener("hashchange",function (e) {
        var now=location.hash && location.hash.substring(1);
        switch (now){
            case "blue":
                document.getElementById("content").setAttribute("class","colorBlue");
                break;
            case "gray":
                document.getElementById("content").setAttribute("class","colorgray");
                break;
            case "green":
                document.getElementById("content").setAttribute("class","colorgreen");
                break;
        }

    },false);
    document.getElementsByClassName("btn")[0].addEventListener("click",function () {
        var now=location.hash && location.hash.substring(1);
        if(now=="blue"){
            location.hash="gray"
            document.getElementById("content").setAttribute("class","colorgray");
        }else if(now=="gray"){
            location.hash="green"
            document.getElementById("content").setAttribute("class","colorgreen");
        }else if(now=="green"){
            location.hash="blue"
            document.getElementById("content").setAttribute("class","colorBlue");
        }
    },false);
</script>
</body>
</html>

在瀏覽器中打開該頁面,並在路由上加上#blue,以下:瀏覽器

clipboard.png

可看到以下頁面,初始條件下,頁面的顯示加載中...,然後定時器觸發更新,顯示遞增的數字,此時咱們能夠在控制檯中打印出對應的history.length,其值爲2:測試

clipboard.png

clipboard.png

接下來,咱們經過點擊change-url 按鈕,去實現修改hash值,咱們能夠看到,對應的路徑發生了改變,#blue變爲#g'ra,背景顏色也對應的更改,但此時遞增的數字沒有被刷新,說明咱們的頁面並無通過刷新重載的過程。url

clipboard.png

clipboard.png

從新在控制檯輸入window.history.length能夠看到,其值已經變爲3,點擊瀏覽器後退箭頭,頁面背景改成以前的藍色背景,到這裏,咱們就實現咱們想要的功能;spa

clipboard.png

history.pushState

除了上面講到的方法外,經過html5新增的history.pushState也能夠實現一樣的效果;
history.pushState和history.replaceState同是html5新增的api,均可以實現改變狀態欄的url而不刷新頁面,但二者的區別是,replaceState是替換當前地址爲指定的url,而pushState則是建立一條新的歷史紀錄。執行history.back()和history.forward()後會觸發window.onpopstate事件。code

API

history.pushState(state,title,url)
state:對象,能夠存放一些數據表示當前的狀態。當瀏覽器執行前進或在後退的時候,會觸發onpopState事件,state將會是event對象的屬性對象,能夠經過event.state訪問;須要注意的是,statez中的屬性值不能爲對象。url爲將要替換的地址;若是是puhState則會添加一條歷史記錄;htm

例子

咱們一樣能夠用上面的例子來測試,只不過,咱們須要監聽的是popstate事件,新建歷史記錄,將當前信息保存到history.state中,對象

history.pushState && window.addEventListener("popstate",function(e){})
history.pushState && history.pushState(state,title,url)

總結

以上介紹的兩種方法,均可以實現頁面不跳轉的前提下,修改url 並新增一條新的歷史記錄,能夠經過瀏覽器的默認行文執行前進後退操做,但須要注意的是,二者監聽的觸發修改後的響應事件不一樣,且修改url的方式也不同。

相關文章
相關標籤/搜索