2 * Originally based of this code... - refactored for Roo...
5 * @author Benjamin Arthur Lupton <contact@balupton.com>
6 * @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
7 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
13 // ====================================================================
19 * How long should the interval be before hashchange checks
21 thishashChangeInterval : 100,
25 * How long should the interval be before safari poll checks
27 safariPollInterval : 500,
30 * History.options.doubleCheckInterval
31 * How long should the interval be before we perform a double check
33 doubleCheckInterval : 500,
36 * History.options.disableSuid
37 * Force this.not to append suid
42 * History.options.storeInterval
43 * How long should we wait between store calls
48 * History.options.busyDelay
49 * How long should we wait between busy events
54 * History.options.debug
55 * If true will enable debug messages to be logged
60 * History.options.initialTitle
61 * What is the title of the initial state
66 * History.options.html4Mode
67 * If true, will force HTMl4 mode (hashtags)
72 * History.options.delayInit
73 * Want to override default options and call init manually.
79 * Which bugs are present
83 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
84 * https://bugs.webkit.org/show_bug.cgi?id=56249
89 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
90 * https://bugs.webkit.org/show_bug.cgi?id=42940
95 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
100 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
105 // ========================================================================
111 sessionStorage : false, // sessionStorage
113 intervalList : false, // array normally.
116 // ====================================================================
121 * The store for all session specific data
127 * 1-1: State ID to State Object
133 * 1-1: State String to State ID
139 * 1-1: State URL to State ID
144 * History.storedStates
145 * Store the states in an array
147 storedStates : false,
150 * History.savedStates
151 * Saved the states in an array
157 // Initialise History
158 init : function(options){
160 initialTitle : window.document.title,
165 this.storedStates=[];
169 Roo.apply(this,options)
171 // Check Load Status of Adapter
172 //if ( typeof this.Adapter === 'undefined' ) {
176 // Check Load Status of Core
177 if ( typeof this.initCore !== 'undefined' ) {
181 // Check Load Status of HTML4 Support
182 if ( typeof this.initHtml4 !== 'undefined' ) {
189 * Is History enabled?
191 this.enabled = !this.emulated.pushState;
204 // ========================================================================
208 initCore : function(options){
210 this.intervalList = [];
214 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
215 this.sessionStorage.setItem('TEST', '1');
216 this.sessionStorage.removeItem('TEST');
218 this.sessionStorage = false;
222 if ( typeof this.initCore.initialized !== 'undefined' ) {
227 this.initCore.initialized = true;
235 * History.clearAllIntervals
236 * Clears all setInterval instances.
238 clearAllIntervals: function()
240 var i, il = this.intervalList;
241 if (typeof il !== "undefined" && il !== null) {
242 for (i = 0; i < il.length; i++) {
243 clearInterval(il[i]);
245 this.intervalList = null;
250 // ====================================================================
254 * History.debugLog(message,...)
255 * Logs the passed arguments if debug enabled
257 debugLog : function()
259 if ( (this.debug||false) ) {
260 Roo.log.apply(History,arguments);
266 // ====================================================================
270 * History.getInternetExplorerMajorVersion()
271 * Get's the major version of Internet Explorer
273 * @license Public Domain
274 * @author Benjamin Arthur Lupton <contact@balupton.com>
275 * @author James Padolsey <https://gist.github.com/527683>
277 getInternetExplorerMajorVersion : function(){
278 var result = this.getInternetExplorerMajorVersion.cached =
279 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
280 ? this.getInternetExplorerMajorVersion.cached
283 div = window.document.createElement('div'),
284 all = div.getElementsByTagName('i');
285 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
286 return (v > 4) ? v : false;
293 * isInternetExplorer()
294 * Are we using Internet Explorer?
296 * @license Public Domain
297 * @author Benjamin Arthur Lupton <contact@balupton.com>
299 isInternetExplorer : function(){
301 this.isInternetExplorer.cached =
302 (typeof this.isInternetExplorer.cached !== 'undefined')
303 ? this.isInternetExplorer.cached
304 : Boolean(this.getInternetExplorerMajorVersion())
310 * Which features require emulating?
318 initEmulated : function()
322 if (this.html4Mode) {
328 this.emulated.pushState = !Boolean(
329 window.history && window.history.pushState && window.history.replaceState
331 (/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i).test(window.navigator.userAgent) /* disable for versions of iOS before version 4.3 (8F190) */
332 || (/AppleWebKit\/5([0-2]|3[0-2])/i).test(window.navigator.userAgent) /* disable for the mercury iOS browser, or at least older versions of the webkit engine */
335 this.emulated.hashChange = Boolean(
336 !(('onhashchange' in window) || ('onhashchange' in window.document))
338 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
347 * Checks to see if the Object is Empty
348 * @param {Object} obj
351 isEmptyObject = function(obj) {
352 for ( var name in obj ) {
353 if ( obj.hasOwnProperty(name) ) {
362 * Clones a object and eliminate all references to the original contexts
363 * @param {Object} obj
366 cloneObject = function(obj) {
369 hash = JSON.stringify(obj);
370 newObj = JSON.parse(hash);
379 // ====================================================================
384 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
385 * @return {String} rootUrl
387 getRootUrl = function(){
389 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
390 if ( window.document.location.port||false ) {
391 rootUrl += ':'+window.document.location.port;
401 * Fetches the `href` attribute of the `<base href="...">` element if it exists
402 * @return {String} baseHref
404 getBaseHref = function(){
407 baseElements = window.document.getElementsByTagName('base'),
411 // Test for Base Element
412 if ( baseElements.length === 1 ) {
413 // Prepare for Base Element
414 baseElement = baseElements[0];
415 baseHref = baseElement.href.replace(/[^\/]+$/,'');
418 // Adjust trailing slash
419 baseHref = baseHref.replace(/\/+$/,'');
420 if ( baseHref ) baseHref += '/';
428 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
429 * @return {String} baseUrl
431 getBaseUrl = function(){
433 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
441 * Fetches the URL of the current page
442 * @return {String} pageUrl
444 getPageUrl = function(){
447 State = this.getState(false,false),
448 stateUrl = (State||{}).url||this.getLocationHref(),
452 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
453 return (/\./).test(part) ? part : part+'/';
462 * Fetches the Url of the directory of the current page
463 * @return {String} basePageUrl
465 getBasePageUrl = function(){
467 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
468 return (/[^\/]$/).test(part) ? '' : part;
469 }).replace(/\/+$/,'')+'/';
477 * Ensures that we have an absolute URL and not a relative URL
478 * @param {string} url
479 * @param {Boolean} allowBaseHref
480 * @return {string} fullUrl
482 getFullUrl = function(url,allowBaseHref){
484 var fullUrl = url, firstChar = url.substring(0,1);
485 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
488 if ( /[a-z]+\:\/\//.test(url) ) {
491 else if ( firstChar === '/' ) {
493 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
495 else if ( firstChar === '#' ) {
497 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
499 else if ( firstChar === '?' ) {
501 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
505 if ( allowBaseHref ) {
506 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
508 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
510 // We have an if condition above as we do not want hashes
511 // which are relative to the baseHref in our URLs
512 // as if the baseHref changes, then all our bookmarks
513 // would now point to different locations
514 // whereas the basePageUrl will always stay the same
518 return fullUrl.replace(/\#$/,'');
523 * Ensures that we have a relative URL and not a absolute URL
524 * @param {string} url
525 * @return {string} url
527 getShortUrl = function(url){
529 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
532 if ( this.emulated.pushState ) {
533 // We are in a if statement as when pushState is not emulated
534 // The actual url these short urls are relative to can change
535 // So within the same session, we the url may end up somewhere different
536 shortUrl = shortUrl.replace(baseUrl,'');
540 shortUrl = shortUrl.replace(rootUrl,'/');
542 // Ensure we can still detect it as a state
543 if ( this.isTraditionalAnchor(shortUrl) ) {
544 shortUrl = './'+shortUrl;
548 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
555 * getLocationHref(document)
556 * Returns a normalized version of document.location.href
557 * accounting for browser inconsistencies, etc.
559 * This URL will be URI-encoded and will include the hash
561 * @param {object} document
562 * @return {string} url
564 getLocationHref = function(doc) {
565 doc = doc || window.document;
567 // most of the time, this will be true
568 if (doc.URL === doc.location.href)
569 return doc.location.href;
571 // some versions of webkit URI-decode document.location.href
572 // but they leave document.URL in an encoded state
573 if (doc.location.href === decodeURIComponent(doc.URL))
576 // FF 3.6 only updates document.URL when a page is reloaded
577 // document.location.href is updated correctly
578 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
579 return doc.location.href;
581 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
582 return doc.location.href;
584 return doc.URL || doc.location.href;
591 * Noramlize the store by adding necessary values
593 normalizeStore = function(){
594 this.store.idToState = this.store.idToState||{};
595 this.store.urlToId = this.store.urlToId||{};
596 this.store.stateToId = this.store.stateToId||{};
601 * Get an object containing the data, title and url of the current state
602 * @param {Boolean} friendly
603 * @param {Boolean} create
604 * @return {Object} State
606 getState : function(friendly,create){
608 if ( typeof friendly === 'undefined' ) { friendly = true; }
609 if ( typeof create === 'undefined' ) { create = true; }
612 var State = History.getLastSavedState();
615 if ( !State && create ) {
616 State = History.createStateObject();
621 State = History.cloneObject(State);
622 State.url = State.cleanUrl||State.url;
630 * History.getIdByState(State)
631 * Gets a ID for a State
632 * @param {State} newState
633 * @return {String} id
635 History.getIdByState = function(newState){
638 var id = History.extractId(newState.url),
642 // Find ID via State String
643 str = History.getStateString(newState);
644 if ( typeof History.stateToId[str] !== 'undefined' ) {
645 id = History.stateToId[str];
647 else if ( typeof History.store.stateToId[str] !== 'undefined' ) {
648 id = History.store.stateToId[str];
653 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
654 if ( typeof History.idToState[id] === 'undefined' && typeof History.store.idToState[id] === 'undefined' ) {
659 // Apply the new State to the ID
660 History.stateToId[str] = id;
661 History.idToState[id] = newState;
670 * History.normalizeState(State)
671 * Expands a State Object
672 * @param {object} State
675 History.normalizeState = function(oldState){
677 var newState, dataNotEmpty;
680 if ( !oldState || (typeof oldState !== 'object') ) {
685 if ( typeof oldState.normalized !== 'undefined' ) {
690 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
694 // ----------------------------------------------------------------
698 newState.normalized = true;
699 newState.title = oldState.title||'';
700 newState.url = History.getFullUrl(oldState.url?oldState.url:(History.getLocationHref()));
701 newState.hash = History.getShortUrl(newState.url);
702 newState.data = History.cloneObject(oldState.data);
705 newState.id = History.getIdByState(newState);
707 // ----------------------------------------------------------------
710 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
711 newState.url = newState.cleanUrl;
713 // Check to see if we have more than just a url
714 dataNotEmpty = !History.isEmptyObject(newState.data);
717 if ( (newState.title || dataNotEmpty) && History.options.disableSuid !== true ) {
719 newState.hash = History.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
720 if ( !/\?/.test(newState.hash) ) {
721 newState.hash += '?';
723 newState.hash += '&_suid='+newState.id;
726 // Create the Hashed URL
727 newState.hashedUrl = History.getFullUrl(newState.hash);
729 // ----------------------------------------------------------------
731 // Update the URL if we have a duplicate
732 if ( (History.emulated.pushState || History.bugs.safariPoll) && History.hasUrlDuplicate(newState) ) {
733 newState.url = newState.hashedUrl;
736 // ----------------------------------------------------------------
743 * History.createStateObject(data,title,url)
744 * Creates a object based on the data, title and url state params
745 * @param {object} data
746 * @param {string} title
747 * @param {string} url
750 History.createStateObject = function(data,title,url){
759 State = History.normalizeState(State);
766 * History.getStateById(id)
767 * Get a state by it's UID
770 History.getStateById = function(id){
775 var State = History.idToState[id] || History.store.idToState[id] || undefined;
782 * Get a State's String
783 * @param {State} passedState
785 History.getStateString = function(passedState){
787 var State, cleanedState, str;
790 State = History.normalizeState(passedState);
795 title: passedState.title,
800 str = JSON.stringify(cleanedState);
808 * @param {State} passedState
809 * @return {String} id
811 History.getStateId = function(passedState){
816 State = History.normalizeState(passedState);
826 * History.getHashByState(State)
827 * Creates a Hash for the State Object
828 * @param {State} passedState
829 * @return {String} hash
831 History.getHashByState = function(passedState){
836 State = History.normalizeState(passedState);
846 * History.extractId(url_or_hash)
847 * Get a State ID by it's URL or Hash
848 * @param {string} url_or_hash
849 * @return {string} id
851 History.extractId = function ( url_or_hash ) {
853 var id,parts,url, tmp;
857 // If the URL has a #, use the id from before the #
858 if (url_or_hash.indexOf('#') != -1)
860 tmp = url_or_hash.split("#")[0];
867 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
868 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
869 id = parts ? String(parts[2]||'') : '';
876 * History.isTraditionalAnchor
877 * Checks to see if the url is a traditional anchor or not
878 * @param {String} url_or_hash
881 History.isTraditionalAnchor = function(url_or_hash){
883 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
886 return isTraditional;
890 * History.extractState
891 * Get a State by it's URL or Hash
892 * @param {String} url_or_hash
893 * @return {State|null}
895 History.extractState = function(url_or_hash,create){
897 var State = null, id, url;
898 create = create||false;
901 id = History.extractId(url_or_hash);
903 State = History.getStateById(id);
906 // Fetch SUID returned no State
909 url = History.getFullUrl(url_or_hash);
912 id = History.getIdByUrl(url)||false;
914 State = History.getStateById(id);
918 if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) {
919 State = History.createStateObject(null,null,url);
928 * History.getIdByUrl()
929 * Get a State ID by a State URL
931 History.getIdByUrl = function(url){
933 var id = History.urlToId[url] || History.store.urlToId[url] || undefined;
940 * History.getLastSavedState()
941 * Get an object containing the data, title and url of the current state
942 * @return {Object} State
944 History.getLastSavedState = function(){
945 return History.savedStates[History.savedStates.length-1]||undefined;
949 * History.getLastStoredState()
950 * Get an object containing the data, title and url of the current state
951 * @return {Object} State
953 History.getLastStoredState = function(){
954 return History.storedStates[History.storedStates.length-1]||undefined;
958 * History.hasUrlDuplicate
959 * Checks if a Url will have a url conflict
960 * @param {Object} newState
961 * @return {Boolean} hasDuplicate
963 History.hasUrlDuplicate = function(newState) {
965 var hasDuplicate = false,
969 oldState = History.extractState(newState.url);
972 hasDuplicate = oldState && oldState.id !== newState.id;
981 * @param {Object} newState
982 * @return {Object} newState
984 History.storeState = function(newState){
986 History.urlToId[newState.url] = newState.id;
989 History.storedStates.push(History.cloneObject(newState));
996 * History.isLastSavedState(newState)
997 * Tests to see if the state is the last state
998 * @param {Object} newState
999 * @return {boolean} isLast
1001 History.isLastSavedState = function(newState){
1004 newId, oldState, oldId;
1007 if ( History.savedStates.length ) {
1008 newId = newState.id;
1009 oldState = History.getLastSavedState();
1010 oldId = oldState.id;
1013 isLast = (newId === oldId);
1023 * @param {Object} newState
1024 * @return {boolean} changed
1026 History.saveState = function(newState){
1028 if ( History.isLastSavedState(newState) ) {
1033 History.savedStates.push(History.cloneObject(newState));
1040 * History.getStateByIndex()
1041 * Gets a state by the index
1042 * @param {integer} index
1045 History.getStateByIndex = function(index){
1050 if ( typeof index === 'undefined' ) {
1051 // Get the last inserted
1052 State = History.savedStates[History.savedStates.length-1];
1054 else if ( index < 0 ) {
1056 State = History.savedStates[History.savedStates.length+index];
1059 // Get from the beginning
1060 State = History.savedStates[index];
1068 * History.getCurrentIndex()
1069 * Gets the current index
1072 History.getCurrentIndex = function(){
1077 if(History.savedStates.length < 1) {
1081 index = History.savedStates.length-1;
1086 // ====================================================================
1091 * @param {Location=} location
1092 * Gets the current document hash
1093 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1096 History.getHash = function(doc){
1097 var url = History.getLocationHref(doc),
1099 hash = History.getHashByUrl(url);
1104 * History.unescapeHash()
1105 * normalize and Unescape a Hash
1106 * @param {String} hash
1109 History.unescapeHash = function(hash){
1111 var result = History.normalizeHash(hash);
1114 result = decodeURIComponent(result);
1121 * History.normalizeHash()
1122 * normalize a hash across browsers
1125 History.normalizeHash = function(hash){
1127 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1134 * History.setHash(hash)
1135 * Sets the document hash
1136 * @param {string} hash
1139 History.setHash = function(hash,queue){
1144 if ( queue !== false && History.busy() ) {
1145 // Wait + Push to Queue
1146 //History.debug('History.setHash: we must wait', arguments);
1149 callback: History.setHash,
1157 //History.debug('History.setHash: called',hash);
1159 // Make Busy + Continue
1162 // Check if hash is a state
1163 State = History.extractState(hash,true);
1164 if ( State && !History.emulated.pushState ) {
1165 // Hash is a state so skip the setHash
1166 //History.debug('History.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1169 History.pushState(State.data,State.title,State.url,false);
1171 else if ( History.getHash() !== hash ) {
1172 // Hash is a proper hash, so apply it
1174 // Handle browser bugs
1175 if ( History.bugs.setHash ) {
1176 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1178 // Fetch the base page
1179 pageUrl = History.getPageUrl();
1181 // Safari hash apply
1182 History.pushState(null,null,pageUrl+'#'+hash,false);
1185 // Normal hash apply
1186 window.document.location.hash = hash;
1196 * normalize and Escape a Hash
1199 History.escapeHash = function(hash){
1201 var result = History.normalizeHash(hash);
1204 result = window.encodeURIComponent(result);
1207 if ( !History.bugs.hashEscape ) {
1208 // Restore common parts
1210 .replace(/\%21/g,'!')
1211 .replace(/\%26/g,'&')
1212 .replace(/\%3D/g,'=')
1213 .replace(/\%3F/g,'?');
1221 * History.getHashByUrl(url)
1222 * Extracts the Hash from a URL
1223 * @param {string} url
1224 * @return {string} url
1226 History.getHashByUrl = function(url){
1228 var hash = String(url)
1229 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1233 hash = History.unescapeHash(hash);
1240 * History.setTitle(title)
1241 * Applies the title to the document
1242 * @param {State} newState
1245 History.setTitle = function(newState){
1247 var title = newState.title,
1252 firstState = History.getStateByIndex(0);
1253 if ( firstState && firstState.url === newState.url ) {
1254 title = firstState.title||History.options.initialTitle;
1260 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1262 catch ( Exception ) { }
1263 window.document.title = title;
1270 // ====================================================================
1275 * The list of queues to use
1276 * First In, First Out
1278 History.queues = [];
1281 * History.busy(value)
1282 * @param {boolean} value [optional]
1283 * @return {boolean} busy
1285 History.busy = function(value){
1287 if ( typeof value !== 'undefined' ) {
1288 //History.debug('History.busy: changing ['+(History.busy.flag||false)+'] to ['+(value||false)+']', History.queues.length);
1289 History.busy.flag = value;
1292 else if ( typeof History.busy.flag === 'undefined' ) {
1293 History.busy.flag = false;
1297 if ( !History.busy.flag ) {
1298 // Execute the next item in the queue
1299 window.clearTimeout(History.busy.timeout);
1300 var fireNext = function(){
1302 if ( History.busy.flag ) return;
1303 for ( i=History.queues.length-1; i >= 0; --i ) {
1304 queue = History.queues[i];
1305 if ( queue.length === 0 ) continue;
1306 item = queue.shift();
1307 History.fireQueueItem(item);
1308 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1311 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1315 return History.busy.flag;
1321 History.busy.flag = false;
1324 * History.fireQueueItem(item)
1326 * @param {Object} item
1327 * @return {Mixed} result
1329 History.fireQueueItem = function(item){
1330 return item.callback.apply(item.scope||History,item.args||[]);
1334 * History.pushQueue(callback,args)
1335 * Add an item to the queue
1336 * @param {Object} item [scope,callback,args,queue]
1338 History.pushQueue = function(item){
1339 // Prepare the queue
1340 History.queues[item.queue||0] = History.queues[item.queue||0]||[];
1343 History.queues[item.queue||0].push(item);
1350 * History.queue (item,queue), (func,queue), (func), (item)
1351 * Either firs the item now if not busy, or adds it to the queue
1353 History.queue = function(item,queue){
1355 if ( typeof item === 'function' ) {
1360 if ( typeof queue !== 'undefined' ) {
1365 if ( History.busy() ) {
1366 History.pushQueue(item);
1368 History.fireQueueItem(item);
1376 * History.clearQueue()
1379 History.clearQueue = function(){
1380 History.busy.flag = false;
1381 History.queues = [];
1386 // ====================================================================
1390 * History.stateChanged
1391 * States whether or not the state has changed since the last double check was initialised
1393 History.stateChanged = false;
1396 * History.doubleChecker
1397 * Contains the timeout used for the double checks
1399 History.doubleChecker = false;
1402 * History.doubleCheckComplete()
1403 * Complete a double check
1406 History.doubleCheckComplete = function(){
1408 History.stateChanged = true;
1411 History.doubleCheckClear();
1418 * History.doubleCheckClear()
1419 * Clear a double check
1422 History.doubleCheckClear = function(){
1424 if ( History.doubleChecker ) {
1425 window.clearTimeout(History.doubleChecker);
1426 History.doubleChecker = false;
1434 * History.doubleCheck()
1435 * Create a double check
1438 History.doubleCheck = function(tryAgain){
1440 History.stateChanged = false;
1441 History.doubleCheckClear();
1443 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1444 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1445 if ( History.bugs.ieDoubleCheck ) {
1447 History.doubleChecker = window.setTimeout(
1449 History.doubleCheckClear();
1450 if ( !History.stateChanged ) {
1451 //History.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1457 History.options.doubleCheckInterval
1466 // ====================================================================
1470 * History.safariStatePoll()
1471 * Poll the current state
1474 History.safariStatePoll = function(){
1477 // Get the Last State which has the new URL
1479 urlState = History.extractState(History.getLocationHref()),
1482 // Check for a difference
1483 if ( !History.isLastSavedState(urlState) ) {
1484 newState = urlState;
1490 // Check if we have a state with that url
1493 //History.debug('History.safariStatePoll: new');
1494 newState = History.createStateObject();
1497 // Apply the New State
1498 //History.debug('History.safariStatePoll: trigger');
1499 History.Adapter.trigger(window,'popstate');
1506 // ====================================================================
1510 * History.back(queue)
1511 * Send the browser history back one item
1512 * @param {Integer} queue [optional]
1514 History.back = function(queue){
1515 //History.debug('History.back: called', arguments);
1518 if ( queue !== false && History.busy() ) {
1519 // Wait + Push to Queue
1520 //History.debug('History.back: we must wait', arguments);
1523 callback: History.back,
1530 // Make Busy + Continue
1533 // Fix certain browser bugs that prevent the state from changing
1534 History.doubleCheck(function(){
1535 History.back(false);
1546 * History.forward(queue)
1547 * Send the browser history forward one item
1548 * @param {Integer} queue [optional]
1550 History.forward = function(queue){
1551 //History.debug('History.forward: called', arguments);
1554 if ( queue !== false && History.busy() ) {
1555 // Wait + Push to Queue
1556 //History.debug('History.forward: we must wait', arguments);
1559 callback: History.forward,
1566 // Make Busy + Continue
1569 // Fix certain browser bugs that prevent the state from changing
1570 History.doubleCheck(function(){
1571 History.forward(false);
1577 // End forward closure
1582 * History.go(index,queue)
1583 * Send the browser history back or forward index times
1584 * @param {Integer} queue [optional]
1586 History.go = function(index,queue){
1587 //History.debug('History.go: called', arguments);
1595 for ( i=1; i<=index; ++i ) {
1596 History.forward(queue);
1599 else if ( index < 0 ) {
1601 for ( i=-1; i>=index; --i ) {
1602 History.back(queue);
1606 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1614 // ====================================================================
1615 // HTML5 State Support
1617 // Non-Native pushState Implementation
1618 if ( History.emulated.pushState ) {
1620 * Provide Skeleton for HTML4 Browsers
1624 var emptyFunction = function(){};
1625 History.pushState = History.pushState||emptyFunction;
1626 History.replaceState = History.replaceState||emptyFunction;
1627 } // History.emulated.pushState
1629 // Native pushState Implementation
1632 * Use native HTML5 History API Implementation
1636 * History.onPopState(event,extra)
1637 * Refresh the Current State
1639 History.onPopState = function(event,extra){
1641 var stateId = false, newState = false, currentHash, currentState;
1643 // Reset the double check
1644 History.doubleCheckComplete();
1646 // Check for a Hash, and handle apporiatly
1647 currentHash = History.getHash();
1648 if ( currentHash ) {
1650 currentState = History.extractState(currentHash||History.getLocationHref(),true);
1651 if ( currentState ) {
1652 // We were able to parse it, it must be a State!
1653 // Let's forward to replaceState
1654 //History.debug('History.onPopState: state anchor', currentHash, currentState);
1655 History.replaceState(currentState.data, currentState.title, currentState.url, false);
1658 // Traditional Anchor
1659 //History.debug('History.onPopState: traditional anchor', currentHash);
1660 History.Adapter.trigger(window,'anchorchange');
1661 History.busy(false);
1664 // We don't care for hashes
1665 History.expectedStateId = false;
1670 stateId = History.Adapter.extractEventData('state',event,extra) || false;
1674 // Vanilla: Back/forward button was used
1675 newState = History.getStateById(stateId);
1677 else if ( History.expectedStateId ) {
1678 // Vanilla: A new state was pushed, and popstate was called manually
1679 newState = History.getStateById(History.expectedStateId);
1683 newState = History.extractState(History.getLocationHref());
1686 // The State did not exist in our store
1688 // Regenerate the State
1689 newState = History.createStateObject(null,null,History.getLocationHref());
1693 History.expectedStateId = false;
1695 // Check if we are the same state
1696 if ( History.isLastSavedState(newState) ) {
1697 // There has been no change (just the page's hash has finally propagated)
1698 //History.debug('History.onPopState: no change', newState, History.savedStates);
1699 History.busy(false);
1704 History.storeState(newState);
1705 History.saveState(newState);
1707 // Force update of the title
1708 History.setTitle(newState);
1711 History.Adapter.trigger(window,'statechange');
1712 History.busy(false);
1717 History.Adapter.bind(window,'popstate',History.onPopState);
1720 * History.pushState(data,title,url)
1721 * Add a new State to the history object, become it, and trigger onpopstate
1722 * We have to trigger for HTML4 compatibility
1723 * @param {object} data
1724 * @param {string} title
1725 * @param {string} url
1728 History.pushState = function(data,title,url,queue){
1729 //History.debug('History.pushState: called', arguments);
1732 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1733 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1737 if ( queue !== false && History.busy() ) {
1738 // Wait + Push to Queue
1739 //History.debug('History.pushState: we must wait', arguments);
1742 callback: History.pushState,
1749 // Make Busy + Continue
1752 // Create the newState
1753 var newState = History.createStateObject(data,title,url);
1756 if ( History.isLastSavedState(newState) ) {
1757 // Won't be a change
1758 History.busy(false);
1761 // Store the newState
1762 History.storeState(newState);
1763 History.expectedStateId = newState.id;
1765 // Push the newState
1766 history.pushState(newState.id,newState.title,newState.url);
1769 History.Adapter.trigger(window,'popstate');
1772 // End pushState closure
1777 * History.replaceState(data,title,url)
1778 * Replace the State and trigger onpopstate
1779 * We have to trigger for HTML4 compatibility
1780 * @param {object} data
1781 * @param {string} title
1782 * @param {string} url
1785 History.replaceState = function(data,title,url,queue){
1786 //History.debug('History.replaceState: called', arguments);
1789 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1790 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1794 if ( queue !== false && History.busy() ) {
1795 // Wait + Push to Queue
1796 //History.debug('History.replaceState: we must wait', arguments);
1799 callback: History.replaceState,
1806 // Make Busy + Continue
1809 // Create the newState
1810 var newState = History.createStateObject(data,title,url);
1813 if ( History.isLastSavedState(newState) ) {
1814 // Won't be a change
1815 History.busy(false);
1818 // Store the newState
1819 History.storeState(newState);
1820 History.expectedStateId = newState.id;
1822 // Push the newState
1823 history.replaceState(newState.id,newState.title,newState.url);
1826 History.Adapter.trigger(window,'popstate');
1829 // End replaceState closure
1833 } // !History.emulated.pushState
1836 // ====================================================================
1842 if ( sessionStorage ) {
1845 History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
1852 History.normalizeStore();
1857 History.normalizeStore();
1861 * Clear Intervals on exit to prevent memory leaks
1863 History.Adapter.bind(window,"unload",History.clearAllIntervals);
1866 * Create the initial State
1868 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
1871 * Bind for Saving Store
1873 if ( sessionStorage ) {
1874 // When the page is closed
1875 History.onUnload = function(){
1877 var currentStore, item, currentStoreString;
1881 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1888 currentStore.idToState = currentStore.idToState || {};
1889 currentStore.urlToId = currentStore.urlToId || {};
1890 currentStore.stateToId = currentStore.stateToId || {};
1893 for ( item in History.idToState ) {
1894 if ( !History.idToState.hasOwnProperty(item) ) {
1897 currentStore.idToState[item] = History.idToState[item];
1899 for ( item in History.urlToId ) {
1900 if ( !History.urlToId.hasOwnProperty(item) ) {
1903 currentStore.urlToId[item] = History.urlToId[item];
1905 for ( item in History.stateToId ) {
1906 if ( !History.stateToId.hasOwnProperty(item) ) {
1909 currentStore.stateToId[item] = History.stateToId[item];
1913 History.store = currentStore;
1914 History.normalizeStore();
1916 // In Safari, going into Private Browsing mode causes the
1917 // Session Storage object to still exist but if you try and use
1918 // or set any property/function of it it throws the exception
1919 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1920 // add something to storage that exceeded the quota." infinitely
1922 currentStoreString = JSON.stringify(currentStore);
1925 sessionStorage.setItem('History.store', currentStoreString);
1928 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1929 if (sessionStorage.length) {
1930 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1931 // removing/resetting the storage can work.
1932 sessionStorage.removeItem('History.store');
1933 sessionStorage.setItem('History.store', currentStoreString);
1935 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1943 // For Internet Explorer
1944 History.intervalList.push(setInterval(History.onUnload,History.options.storeInterval));
1946 // For Other Browsers
1947 History.Adapter.bind(window,'beforeunload',History.onUnload);
1948 History.Adapter.bind(window,'unload',History.onUnload);
1950 // Both are enabled for consistency
1953 // Non-Native pushState Implementation
1954 if ( !History.emulated.pushState ) {
1955 // Be aware, the following is only for native pushState implementations
1956 // If you are wanting to include something for all browsers
1957 // Then include it above this if block
1962 if ( History.bugs.safariPoll ) {
1963 History.intervalList.push(setInterval(History.safariStatePoll, History.options.safariPollInterval));
1967 * Ensure Cross Browser Compatibility
1969 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1971 * Fix Safari HashChange Issue
1975 History.Adapter.bind(window,'hashchange',function(){
1976 History.Adapter.trigger(window,'popstate');
1980 if ( History.getHash() ) {
1981 History.Adapter.onDomLoad(function(){
1982 History.Adapter.trigger(window,'hashchange');
1987 } // !History.emulated.pushState
1990 }; // History.initCore
1992 // Try to Initialise History
1993 if (!History.options || !History.options.delayInit) {