一个简单的例子看明白如何利用window.location.hash实现ajax操作时浏览器的前进/后退功能

我们知道JavaScript中很早就提供了window.history对象,利用history对象的forward()、Go()、back()方法能够方便实现不同页面之间的前进、后退等这种导航功能。但是AJAX操作,是不能用浏览器的前进和后退按钮进行导航的,因为浏览器并不会将AJAX操作加入到历史记录中。但是借助location.hash,我们能够自己实现AJAX操作的前进和后退。关于window.location.hash的详细介绍和使用方式,可以参考下面这2篇文章。

location.hash详解和   6 Things You Should Know About Fragment URLs

我们需要知道以下2点:

1.如果location.hash发生了变化,那么浏览器地址栏url会发生变化,而且浏览器会产生1个历史记录。

2.如果location.hash发生了变化,会产生一个hashchange事件,我们可以处理这个事件。

[html]  view plain  copy
  1. <!DOCTYPE html>   
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <script type="text/javascript" src="jquery-1.11.1.min.js"></script>       
  6.     <script type="text/javascript">         
  7.       
  8.         var currentPageIndex = 0;  
  9.                   
  10.         window.onload = function(){  
  11.             currentPageIndex = 0;  
  12.             showPageAtIndex(currentPageIndex);  
  13.             recordHash(currentPageIndex);  
  14.         }  
  15.           
  16.         // onhashchange可以监控hash变化  
  17.         window.onhashchange=function(){  
  18.             var hash = window.location.hash;  
  19.               
  20.             var id = parseInt(hash.substr(1));  
  21.               
  22.             showPageAtIndex(id);  
  23.         };  
  24.                   
  25.         function toNextPageWhenClick()  
  26.         {  
  27.             currentPageIndex++;  
  28.               
  29.             if(isValidPageIndex(currentPageIndex))  
  30.             {  
  31.                 showPageAtIndex(currentPageIndex);  
  32.                 recordHash(currentPageIndex);  
  33.             }  
  34.             else  
  35.             {  
  36.                 return;  
  37.             }  
  38.         }  
  39.           
  40.         function showPageAtIndex(id)  
  41.         {  
  42.             $("div[id!="+id+"]").hide();  
  43.             $("#"+id).show();  
  44.               
  45.             if(isHomePage(id))  
  46.             {  
  47.                 $("input").attr("value","current is home page,next page=1");  
  48.             }  
  49.             else if(isLastPage(id))  
  50.             {  
  51.                 $("input").attr("value","current page="+id+", it is the end.");  
  52.             }  
  53.             else  
  54.             {  
  55.                 $("input").attr("value","current page="+id+",next page="+(id+1));  
  56.             }  
  57.         }  
  58.           
  59.         function isValidPageIndex(id)  
  60.         {  
  61.             return id <= 5;  
  62.         }  
  63.           
  64.         function isLastPage(id)  
  65.         {  
  66.             return id == 5;  
  67.         }  
  68.           
  69.         function isHomePage(id)  
  70.         {  
  71.             return id == 0;  
  72.         }  
  73.           
  74.         // hash改变,浏览器会自动生成一个历史记录  
  75.         function recordHash(id)  
  76.         {  
  77.             window.location.hash=id;  
  78.         }  
  79.     </script>  
  80.       
  81.     <style>  
  82.         .navigate{  
  83.             height:100px;  
  84.             width:300px;  
  85.             background-color:#0000ff;  
  86.             display:none;  
  87.         }  
  88.           
  89.         .home{  
  90.             height:100px;  
  91.             width:300px;  
  92.             background-color:#00ff00;  
  93.             display:none;  
  94.         }  
  95.           
  96.         .last{  
  97.             height:100px;  
  98.             width:300px;  
  99.             background-color:#ff0000;  
  100.             display:none;  
  101.         }  
  102.     </style>  
  103. </head>   
  104. <body>  
  105.     <input type="button" value="" onclick="toNextPageWhenClick();">  
  106.       
  107.     <div class="home" id="0">HOME PAGE</div>  
  108.     <div class="navigate" id="1">ajax1</div>  
  109.     <div class="navigate" id="2">ajax2</div>  
  110.     <div class="navigate" id="3">ajax3</div>  
  111.     <div class="navigate" id="4">ajax4</div>  
  112.     <div class="last" id="5">last page</div>  
  113. </body>  
  114. </html>  


在chrome下运行这个html文件,默认显示home page,点击按钮的时候回调到下一个页面(直到最后一个页面为止)。我们可以点击浏览器的前进、后退按钮,实现模拟ajax前进、后退的功能。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值