Gmail 对锚点处理的技巧

Gmail 这样的全站 AJAX 应用,依然可以使用浏览器的回退功能,其中的关键是这段代码:

<script>
window.onload = function()
{
    
setInterval("check_anchor()", 100);
}
 
var current_anchor = null;
 
function check_anchor()
{
    
if (current_anchor != document.location.hash)
    
{
          
current_anchor = document.location.hash;
 
          
if (current_anchor == "#inbox")
          
{
              
// Show your inbox section.
          
}
 
          
else if (current_anchor == "#sent")
          
{
              
// Show your sent items section.
          
}
    
}
}
<
script>

原理是不断的检查 Url 里面的锚点,是否被改变了,很小的技巧,不过很实用。

接下来发现 IE 在锚点变化的时候,并不会将用户的操作存为 history,所以回退按钮是无效的,好在有人已经考虑到这浏览器的兼容性,已经做好了用JS修改IE的history的实现:

参考页面:
http://www.mikage.to/jquery/jquery_history.html

JS库:
http://www.mikage.to/jquery/jquery.history.js

YUI 也有一个history管理的库:
http://developer.yahoo.com/yui/history/

整理自:
Creating Gmail-like anchor-based navigation
Save the anchor in the ie6 history

Leave a Reply