2011-12-13 22 views
1

所以我的整個網站運行一個index.php頁面,內容根據用戶想要做的事情動態加載許多AJAX調用。我想使用後退按鈕,但目前我不能。我已經閱讀了一些很棒的教程,並蒐集了一些有關這方面的很棒的Stackoverflow問題。我的問題是這樣的。我看到的所有教程,包括我傾向使用的教程,Jquery BBQ,在所有教程中似乎都使用href,但我根本不使用定位標記。如何實現Ajax後退按鈕模擬沒有href

我的典型功能....說加載評論。

$('.comments').live('click', function() { 
    var id = this.id; 
    // pass the unique id of the class to a php function, return result 
}); 

我知道我會用id跟蹤在URL中的散列的,但我不知道如何做到這一切的時候,我發現是錨標記的例子。有人能指出我正確的方向一個很好的教程,謝謝!

回答

2

可以使用window.location對象hash屬性:

$('.comments').live('click', function() { 
    var id = this.id; 
    window.location.hash = id; 
    // pass the unique id of the class to a php function, return result 
}); 

然後,如果用戶刷新頁面或深層鏈接進入網站:

if (typeof(window.locaion.hash) != 'undefined') { 
    //now you know there is a hash and you can trigger a click on the corresponding element 
    $('#' + window.locaion.hash).trigger('click'); 
} 
+0

觸發正是我我在找,謝謝賈斯帕! – Naterade