public
anonymous / ajax_back_button.js
Created

  • Download Gist
ajax_back_button.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
(function () {
 
var _pageStorage = {};
var _oldToken = null;
 
//we now store the history of this page
if (!window.jsf)
window.jsf = {};
window.jsf.history = {};
 
//we assume a parametrized hash here
 
/*helper routines*/
/**
* gets the hash values, we assume that the hash values can store
* http get parameters for simulated get requests (google ajax query api)
* hence we do more processing than needed
*/
function getHashValues() {
var hash = window.location.hash;
 
//google hash api
if (hash.indexOf("#") == 0) {
hash = hash.substring(1, hash.length);
}
if (hash.indexOf("!") == 0) {
hash = hash.substring(1, hash.length);
}
hash = hash.split("&");
var ret = {};
for (var cnt = hash.length - 1; cnt >= 0; cnt = cnt - 1) {
var vals = hash[cnt].split("=");
ret[vals[0]] = (vals.length > 1) ? vals[1] : null;
}
return ret;
}
 
/**
* makes full hash representation of the hashes map
* @param hashes
*/
function makeHash(hashes) {
var finalHashColl = [];
for (var key in hashes) {
if (!hashes.hasOwnProperty(key)) continue;
finalHashColl.push(key + ((null != hashes[key]) ? "=" + hashes[key] : ""));
}
var ret = finalHashColl.join("&");
if (ret.indexOf("&") == 0) {
ret = ret.substring(1, ret.length);
}
return ret;
}
 
/**
* returns a single value from the hash stored in the url
* @param key
*/
function getHashValue(key) {
return getHashValues()[key];
}
 
/**
* sets a single value into the hash stored in the url
* @param key the key to store the hash
* @param theVal the value
*/
function setHashValue(key, theVal) {
var hashValues = getHashValues();
hashValues[key] = theVal;
window.location.hash = makeHash(hashValues);
 
}
 
/**
* our snapshotting function which snapshots the page
*/
window.jsf.history.snapshotPage = function () {
//no onpopstate and no sessionstorage then we do nothing,
// ie I am talking about you
setTimeout(function () {
if (!window.onpopstate) {
window.jsf.history.setPopstateHandler();
}
 
var stateObj = {state:document.body.innerHTML};
var statusIdx = "" + (new Date()).getTime();
_pageStorage[statusIdx] = document.body.innerHTML;
setHashValue("token", statusIdx);
_oldToken = statusIdx;
}, 10);
}
 
/**
* the pop state handler setting routine
* which sets the simualted pop state function (in our
* case here an onhashchange function)
*/
window.jsf.history.setPopstateHandler = function (handler) {
window.onhashchange = function () {
var token = getHashValue("token");
if (token != _oldToken) {
_oldToken = token;
var statusIdx = getHashValue("token");
var page = _pageStorage[statusIdx];
if (page) {
document.body.innerHTML = page;
}
}
}
}
 
/**
* a onhashchange simulation which checks regularily
* for hash changes
*/
var oldGlobalHash;
setInterval(function () {
//not needed for ie9
if (oldGlobalHash!= window.location.hash) {
oldGlobalHash = window.location.hash;
if (window.onhashchange) {
window.onhashchange({});
}
 
}
}, 1000);
 
/**
* initial snapshotting
*/
window.jsf.history.snapshotPage();
 
/**
* Global jsf event handler which does snapshotting on every success
*/
function theHandler(evt) {
if (evt.status == "success") {
window.jsf.history.snapshotPage();
}
}
 
jsf.ajax.addOnEvent(theHandler);
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.