HTML5 history pushState/replaceState執行個體

來源:互聯網
上載者:User

標籤:

---恢複內容開始---

在移動端,越來越傾向於頁面內跳轉,而頁面內跳轉就需要用到history的管理,html5的history是一種解決方案。

在沒有history ap之前,我們經常使用散列值來改變頁面內容,特別是那些對頁面特別重要的內容。因為沒有重新整理,所以對於單頁面應用,改變其URL是不可能的。此外,當你改變URL的散列值,它對瀏覽器的記錄沒有任何影響。通過增加location.hash,並用onhashchange來達到目的。

現在對於HTML 5的History API來說,這些都是可以輕易實現的,但是由於單頁面應用沒必要使用散列值,它可能需要額外的開發指令碼。它也允許我們用一種對SEO友好的方式建立新應用。

使用history

HTML 5提供了兩個新方法:pushstate跟replaceState,兩個方法參數一樣

params { 

  state:儲存JSON字串,可以用在popstate事件中,可以通過locaton.state擷取。

  title:現在大多數瀏覽器不支援或者忽略這個參數,最好用null代替

  url:任意有效URL,用於更新瀏覽器的地址欄,並不在乎URL是否已經存在地址清單中。更重要的是,它不會重新載入頁面。

}

兩個方法的主要區別就是:pushState()是在history棧中添加一個新的條目,replaceState()是替換當前的記錄值。用pushState的時候會產生一條新的history,replaceState則不會產生。

當我們使用pushState()和replaceState()進行處理時或者點擊瀏覽器的前進後退的時候,popstate事件會被觸發,通過監聽popstate事件可以達到一系列功能。

瀏覽器支援

history在某些瀏覽器上支援還不是特別好,可以引用這個實現相容的js

https://github.com/browserstate/history.js

案例DEMO

通過點擊頁面的導航,實現頁面內跳轉,根據popstate監聽URL的改變顯示不同的頁面.

這種路由方式還需要伺服器端的支援,不然重新整理頁面會出現404

如果入口是index,apache可以在根目錄下建個.htaccess檔案,配置如下,

RewriteEngine OnRewriteBase /RewriteRule ^index\.html$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.html [L]

如果你用的ngnix,配置如下

server {  ...  location / {    try_files $uri /index.html;  }}

index.html

<!DOCTYPE html><html>    <head>        <title>History API</title>        <meta charset="utf-8" />    </head>    <body>        <ul id="menu">            <li><a href="/home">首頁</a></li>            <li><a href="/about">關於我們</a></li>            <li><a href="/blog">部落格</a></li>            <li><a href="/photos">相簿</a></li>        </ul>        <button id="back">Back</button>        <button id="forward">Forward</button>        <div>            Action: <span id="action"></span><br/>            Url: <span id="url"></span><br/>            Description: <span id="description"></span>        </div>    </body></html>

js代碼

        document.addEventListener(‘DOMContentLoaded‘, function(){            var act, historyState;            var menu = document.querySelectorAll(‘li a‘);            historyState = {                home : {                    description : "我是首頁------Index Page"                },                about : {                    description : "關於我們------About Page"                },                blog : {                    description : "部落格頁面------Blog Page"                },                photos : {                    description : "相簿頁面------Photos Page"                }                    };            for( let i =0; i < menu.length; i++ ) {                menu[i].addEventListener(‘click‘, function(e){                    e.preventDefault();                    var hash = menu[i].getAttribute(‘href‘);                    var key = hash.replace(‘/‘,‘‘);                    act = "點擊導航";                    historyState[key].url = key;                    //history.pushState(historyState[key], null, hash);                        history.replaceState(historyState[key], null, hash);                    setStateInfo( historyState[key] );                },false);            }             window.addEventListener(‘popstate‘, function(event){                var state = history.state || event.state || window.event.state;                if( state) setStateInfo(state);            },false);            document.querySelector("#back").addEventListener(‘click‘, function(){                act = "點擊後退按鈕";                history.back();            }, false);            document.querySelector("#forward").addEventListener(‘click‘, function(){                act = "點擊前進按鈕";                history.go(1);            });            function setStateInfo( state ){                //ajax處理不同的資料                document.querySelector(‘#action‘).innerHTML = act;                document.querySelector("#url").innerHTML = state.url;                document.querySelector(‘#description‘).innerHTML = state.description;            }            //第一次載入進來的時候            var path = location.pathname;            var arr = path.split(‘/‘);            var index = arr[arr.length - 1] || ‘home‘                        act = "重新整理頁面";            historyState[index].url =  index;            setStateInfo(historyState[index]);            //history.pushState(historyState[index], null, ‘/home‘)            history.replaceState(historyState[index], null, hash);        },false);   

 通過切換就可以達到頁面跳轉和ajax重新整理的效果,而不再出現hash散列值

 

---恢複內容結束---

HTML5 history pushState/replaceState執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.