Skip to content

Instantly share code, notes, and snippets.

@aulizko
Created January 5, 2011 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aulizko/766134 to your computer and use it in GitHub Desktop.
Save aulizko/766134 to your computer and use it in GitHub Desktop.
History manager (outdated a while)
/**
* History managment, for ajax-based pages
* @class History
* @constructor
*/
History = function () {
var
/**
* @property currentHash
* @private
*/
currentHash,
/**
* @property _callback
* @private
*/
_callback,
historyBackStack,
historyForwardStack,
isFirst,
dontCheck,
check = function () {
var i, hash;
if($.browser.msie) {
// On IE, check for location.hash of iframe
var ihistory = $("#APHistory")[0];
var iframe = ihistory.contentDocument || ihistory.contentWindow.document;
hash = iframe.location.hash;
if(hash != currentHash) {
location.hash = hash;
currentHash = hash;
_callback(hash.replace(/^#/, ''));
}
} else if ($.browser.safari) {
if (dontCheck) {
var historyDelta = history.length - historyBackStack.length;
if (historyDelta) { // back or forward button has been pushed
isFirst = false;
if (historyDelta < i =" 0;" i =" 0;" cachedhash =" historyBackStack[historyBackStack.length" currenthash =" location.hash;">= 0) {
_callback(document.URL.split('#')[1]);
} else {
_callback('');
}
isFirst = true;
}
}
} else {
// otherwise, check for location.hash
hash = location.hash;
if(hash != currentHash) {
currentHash = hash;
_callback(hash.replace(/^#/, ''));
}
}
};
return {
initialize : function (callback) {
_callback = callback;
currentHash = location.hash;
if ($.browser.msie) {
// To stop the callback firing twice during initilization if no hash present
if (currentHash == '') {
currentHash = '#';
}
// add hidden iframe for IE
$("body").prepend('<iframe id="APHistory" style="display: none;"></iframe>');
var iframe = $("#APHistory")[0].contentWindow.document;
iframe.open();
iframe.close();
iframe.location.hash = currentHash;
} else if ($.browser.safari) {
// etablish back/forward stacks
historyBackStack = [];
historyBackStack.length = history.length;
historyForwardStack = [];
isFirst = true;
dontCheck = false;
}
_callback(currentHash.replace(/^#/, ''));
setInterval(check, 100);
},
add : function (hash) {
// This makes the looping function do something
historyBackStack.push(hash);
historyForwardStack.length = 0; // clear forwardStack (true click occured)
isFirst = true;
},
/**
*
* @param hash {String} desiring hash without first #
*/
load: function(hash) {
var newhash;
if ($.browser.safari) {
newhash = hash;
} else {
newhash = '#' + hash;
location.hash = newhash;
}
currentHash = newhash;
if ($.browser.msie) {
var ihistory = $("#APHistory")[0]; // TODO: need contentDocument?
var iframe = ihistory.contentWindow.document;
iframe.open();
iframe.close();
iframe.location.hash = newhash;
_callback(hash);
}
else if ($.browser.safari) {
dontCheck = true;
// Manually keep track of the history values for Safari
this.add(hash);
// Wait a while before allowing checking so that Safari has time to update the "history" object
// correctly (otherwise the check loop would detect a false change in hash).
var fn = function() {AP.History.setCheck(false);};
window.setTimeout(fn, 200);
_callback(hash);
// N.B. "location.hash=" must be the last line of code for Safari as execution stops afterwards.
// By explicitly using the "location.hash" command (instead of using a variable set to "location.hash") the
// URL in the browser and the "history" object are both updated correctly.
location.hash = newhash;
}
else {
_callback(hash);
}
},
/**
* Set need we check, or not.
* @param check {Boolean}
* @protected
*/
setCheck : function (check) {
dontCheck = check;
},
/**
* @method getCurrentHash
* @return {String}
*/
getCurrentHash : function () {
return currentHash;
}
};
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment