views:

37

answers:

0

The following is to fix a bug for this issue:

http://stackoverflow.com/questions/3242512/toggling-between-2-bookmarks-or-see-a-page-and-load-another-page-from-bookmark

The code is:

var IframeImpl = {

    // init: [...]

    check: function() {
        var current_iframeHash = iframeWrapper.get();
        var current_mainHash = locationWrapper.get();
        if (current_mainHash != current_iframeHash) {   // either user loaded bookmark or used Back or Forward button
            if (current_mainHash == _.appState) {       // user used Back or Forward button
                _.appState = current_iframeHash;
                locationWrapper.put(current_iframeHash);
                _.callback(current_iframeHash); 
            } else {                                    // user loaded new bookmark
                _.appState = current_mainHash;  
                iframeWrapper.put(current_mainHash);
                _.callback(current_mainHash);
            }
        }
    },

    // load: [...]

};

This is the full listing of the current code: http://github.com/tkyk/jquery-history-plugin/blob/master/jquery.history.js

This is the reasoning: when it is IE 7, it uses an iframe to keep track of the hash history, because programmatically setting the hash value won't get that new URL with hash into the history, so clicking Back button will bring the user all the way to the previous URL, skipping all the hash changes.

Now, in a "stable state", mainHash == appState == iframeHash, but, as Ajax History and Bookmark is to handle both History change (user clicking on Back and Forward button), and Bookmark (user using bookmark of data fetched by Ajax), there are 2 cases:

1) When user clicks on Back and Forward button, the iframeHash changes, but mainHash and appState do not change. So mainHash == appState in this case.

2) When user uses a bookmark, the mainHash changes, but appState and iframeHash do not change, so appState == iframeHash in this case.

Using the conditions above, whenever we see that iframeHash != mainHash, we know either the user clicked on Back or Forward button, or loaded a bookmark, and which Hash value should we use? So if mainHash == appState, the user clicked on Back or Forward button, so we should use the iframe's Hash. Otherwise, the user used a bookmark (or modified the hash on the main URL), and so in this case, we use the mainHash.