通過history解決ajax不支持前進/后退/刷新


前言:

現在前后端基本都是通過ajax實現前后端接口數據的交互,但是,ajax有個小小的劣勢,即:不支持瀏覽器“后退”和“前進“鍵。

但是,現在我們可以通過H5的histroy屬性 解決ajax在交互請求的這個小bug。

事件描述:

H5增加了一個事件window.onpopstate,當用戶點擊那兩個按鈕就會觸 發這個事件。但是光檢測到這個事件是不夠的,還得能夠傳些參數,也就是說返回到之前那個頁面的時候得知道那個頁面的pageIndex。通過 history的pushState方法可以做到,pushState(pageIndex)將當前頁的pageIndex存起來,再返回到這個 頁面時獲取到這個pageIndex。

window.history.pushState描述:

window.history.pushState(state, title, url);

state對象:是一個JavaScript對象,它關系到由pushState()方法創建出來的新的history實體。用以存儲關於你所要插入到歷史 記錄的條目的相關信息。State對象可以是任何Json字符串。因為firefox會使用用戶的硬盤來存取state對象,這個對象的最大存儲空間為640k。如果大於這個數 值,則pushState()方法會拋出一個異常。

title:firefox現在回忽略這個參數,雖然它可能將來會被使用上。而現在最安全的使用方式是傳一個空字符串,以防止將來的修改。

url:用來傳遞新的history實體的URL,瀏覽器將不會在調用pushState()方法后加載這個URL。也許會過一會嘗試加載這個URL。比如在用戶重啟了瀏覽器后,新的url可以不是絕對路徑。如果是相對路徑,那么它會相對於現有的url。新的url必須和現有的url同域,否則pushState()將拋出異常。這個參數是選填的,如果為空,則會被置為document當前的url。

直接貼代碼:

 1 /**
 2  * Created: Aaron.
 3  * address: http://www.cnblogs.com/aaron-pan/
 4  */
 5 
 6 //var pageIndex=window.history.state===null?0:window.history.state.page;
 7 
 8 (function($,window,undefined){
 9     var loadData={
10         pageIndex:window.history.state===null?1:window.history.state.page,
11         //pageIndex:0,
12         init:function(){
13             this.getData(this.pageIndex);
14             this.nextPage();
15         },
16         getData:function(pageIndex){
17             var that=this;
18             $.ajax({
19                 type:'post',
20                 url:'./data/getMovices'+pageIndex+'.json',
21                 dataType:'json',
22                 async:false,
23                 success:function(data){
24                     that.renderDom(data);
25                 }
26             })
27         },
28         renderDom:function(movies){
29             var bookHtml=
30                 "<table>"+
31                 "<tr>"+
32                 "<th>電影</th>>"+
33                 "<th>導演</th>"+
34                 "<th>上映時間</th>"+
35                 "</tr>";
36             for(var i=0;i<movies.length;i++){
37                 bookHtml +=
38                     "<tr>" +
39                     "   <td>" + movies[i].moviesName + "</td>" +
40                     "   <td><a>" + movies[i].moviesEditor + "</a></td>" +
41                     "   <td>" + movies[i].times + "</td>" +
42                     "</tr>";
43             }
44             bookHtml+="</table>";
45             bookHtml +=
46                 "<button>上一頁</button>" +
47                 "<button class='nextPage'>下一頁</button>";
48             $('body').html(bookHtml);
49         },
50         nextPage:function(){
51             var that=this;
52             $(document).on("click",".nextPage",function(){
53                 that.pageIndex++;
54                 that.getData(that.pageIndex);
55                 window.history.pushState({page:that.pageIndex},null,window.location.href);
56                 //后退and刷新回到首頁 window.history.replaceState({page:that.pageIndex},null,window.location.href);
57             })
58         },
59     };
60     loadData.init();
61     window.addEventListener("popstate",function(event){
62         var page=0;
63         if(event.state!==null){
64             page=event.state.page;
65             console.log('page:'+page);
66         }
67         console.log('page:'+page);
68         loadData.getData(page);
69         loadData.pageIndex=page;
70     })
71 
72 })(jQuery,window,undefined);

通過直接在html頁面調用js文件就可看到運行結果。

運行結果:

這樣就可以達到通過ajax進行交互也能實現監聽前進/后台/刷新的功能了。

 附瀏覽器兼容性:

 

更多參考:

ajax與HTML5 history pushState/replaceState實例:http://www.zhangxinxu.com/wordpress/2013/06/html5-history-api-pushstate-replacestate-ajax/

使用h5的history改善ajax列表請求體驗:http://www.cnblogs.com/yincheng/p/h5-history.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM