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.
115 // Initialise History
116 init : function(options){
118 initialTitle : window.document.title,
120 Roo.apply(this,options)
122 // Check Load Status of Adapter
123 //if ( typeof this.Adapter === 'undefined' ) {
127 // Check Load Status of Core
128 if ( typeof this.initCore !== 'undefined' ) {
132 // Check Load Status of HTML4 Support
133 if ( typeof this.initHtml4 !== 'undefined' ) {
142 // ========================================================================
146 initCore : function(options){
148 this.intervalList = [];
152 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
153 this.sessionStorage.setItem('TEST', '1');
154 this.sessionStorage.removeItem('TEST');
156 this.sessionStorage = false;
160 if ( typeof this.initCore.initialized !== 'undefined' ) {
165 this.initCore.initialized = true;
173 * History.clearAllIntervals
174 * Clears all setInterval instances.
176 clearAllIntervals: function()
178 var i, il = this.intervalList;
179 if (typeof il !== "undefined" && il !== null) {
180 for (i = 0; i < il.length; i++) {
181 clearInterval(il[i]);
183 this.intervalList = null;
188 // ====================================================================
192 * History.debugLog(message,...)
193 * Logs the passed arguments if debug enabled
195 debugLog : function()
197 if ( (this.debug||false) ) {
198 Roo.log.apply(History,arguments);
204 // ====================================================================
208 * History.getInternetExplorerMajorVersion()
209 * Get's the major version of Internet Explorer
211 * @license Public Domain
212 * @author Benjamin Arthur Lupton <contact@balupton.com>
213 * @author James Padolsey <https://gist.github.com/527683>
215 getInternetExplorerMajorVersion = function(){
216 var result = this.getInternetExplorerMajorVersion.cached =
217 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
218 ? this.getInternetExplorerMajorVersion.cached
221 div = window.document.createElement('div'),
222 all = div.getElementsByTagName('i');
223 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
224 return (v > 4) ? v : false;
231 * isInternetExplorer()
232 * Are we using Internet Explorer?
234 * @license Public Domain
235 * @author Benjamin Arthur Lupton <contact@balupton.com>
237 isInternetExplorer = function(){
239 this.isInternetExplorer.cached =
240 (typeof this.isInternetExplorer.cached !== 'undefined')
241 ? this.isInternetExplorer.cached
242 : Boolean(this.getInternetExplorerMajorVersion())
248 * Which features require emulating?
256 initEmulated : function()
260 if (this.html4Mode) {
266 this.emulated.pushState = !Boolean(
267 window.history && window.history.pushState && window.history.replaceState
269 (/ 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) */
270 || (/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 */
273 this.emulated.hashChange = Boolean(
274 !(('onhashchange' in window) || ('onhashchange' in window.document))
276 (History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8)
283 * Is History enabled?
285 History.enabled = !History.emulated.pushState;
290 * History.isEmptyObject(obj)
291 * Checks to see if the Object is Empty
292 * @param {Object} obj
295 History.isEmptyObject = function(obj) {
296 for ( var name in obj ) {
297 if ( obj.hasOwnProperty(name) ) {
305 * History.cloneObject(obj)
306 * Clones a object and eliminate all references to the original contexts
307 * @param {Object} obj
310 History.cloneObject = function(obj) {
313 hash = JSON.stringify(obj);
314 newObj = JSON.parse(hash);
323 // ====================================================================
327 * History.getRootUrl()
328 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
329 * @return {String} rootUrl
331 History.getRootUrl = function(){
333 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
334 if ( window.document.location.port||false ) {
335 rootUrl += ':'+window.document.location.port;
344 * History.getBaseHref()
345 * Fetches the `href` attribute of the `<base href="...">` element if it exists
346 * @return {String} baseHref
348 History.getBaseHref = function(){
351 baseElements = window.document.getElementsByTagName('base'),
355 // Test for Base Element
356 if ( baseElements.length === 1 ) {
357 // Prepare for Base Element
358 baseElement = baseElements[0];
359 baseHref = baseElement.href.replace(/[^\/]+$/,'');
362 // Adjust trailing slash
363 baseHref = baseHref.replace(/\/+$/,'');
364 if ( baseHref ) baseHref += '/';
371 * History.getBaseUrl()
372 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
373 * @return {String} baseUrl
375 History.getBaseUrl = function(){
377 var baseUrl = History.getBaseHref()||History.getBasePageUrl()||History.getRootUrl();
384 * History.getPageUrl()
385 * Fetches the URL of the current page
386 * @return {String} pageUrl
388 History.getPageUrl = function(){
391 State = History.getState(false,false),
392 stateUrl = (State||{}).url||History.getLocationHref(),
396 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
397 return (/\./).test(part) ? part : part+'/';
405 * History.getBasePageUrl()
406 * Fetches the Url of the directory of the current page
407 * @return {String} basePageUrl
409 History.getBasePageUrl = function(){
411 var basePageUrl = (History.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
412 return (/[^\/]$/).test(part) ? '' : part;
413 }).replace(/\/+$/,'')+'/';
420 * History.getFullUrl(url)
421 * Ensures that we have an absolute URL and not a relative URL
422 * @param {string} url
423 * @param {Boolean} allowBaseHref
424 * @return {string} fullUrl
426 History.getFullUrl = function(url,allowBaseHref){
428 var fullUrl = url, firstChar = url.substring(0,1);
429 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
432 if ( /[a-z]+\:\/\//.test(url) ) {
435 else if ( firstChar === '/' ) {
437 fullUrl = History.getRootUrl()+url.replace(/^\/+/,'');
439 else if ( firstChar === '#' ) {
441 fullUrl = History.getPageUrl().replace(/#.*/,'')+url;
443 else if ( firstChar === '?' ) {
445 fullUrl = History.getPageUrl().replace(/[\?#].*/,'')+url;
449 if ( allowBaseHref ) {
450 fullUrl = History.getBaseUrl()+url.replace(/^(\.\/)+/,'');
452 fullUrl = History.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
454 // We have an if condition above as we do not want hashes
455 // which are relative to the baseHref in our URLs
456 // as if the baseHref changes, then all our bookmarks
457 // would now point to different locations
458 // whereas the basePageUrl will always stay the same
462 return fullUrl.replace(/\#$/,'');
466 * History.getShortUrl(url)
467 * Ensures that we have a relative URL and not a absolute URL
468 * @param {string} url
469 * @return {string} url
471 History.getShortUrl = function(url){
473 var shortUrl = url, baseUrl = History.getBaseUrl(), rootUrl = History.getRootUrl();
476 if ( History.emulated.pushState ) {
477 // We are in a if statement as when pushState is not emulated
478 // The actual url these short urls are relative to can change
479 // So within the same session, we the url may end up somewhere different
480 shortUrl = shortUrl.replace(baseUrl,'');
484 shortUrl = shortUrl.replace(rootUrl,'/');
486 // Ensure we can still detect it as a state
487 if ( History.isTraditionalAnchor(shortUrl) ) {
488 shortUrl = './'+shortUrl;
492 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
499 * History.getLocationHref(document)
500 * Returns a normalized version of document.location.href
501 * accounting for browser inconsistencies, etc.
503 * This URL will be URI-encoded and will include the hash
505 * @param {object} document
506 * @return {string} url
508 History.getLocationHref = function(doc) {
509 doc = doc || window.document;
511 // most of the time, this will be true
512 if (doc.URL === doc.location.href)
513 return doc.location.href;
515 // some versions of webkit URI-decode document.location.href
516 // but they leave document.URL in an encoded state
517 if (doc.location.href === decodeURIComponent(doc.URL))
520 // FF 3.6 only updates document.URL when a page is reloaded
521 // document.location.href is updated correctly
522 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
523 return doc.location.href;
525 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
526 return doc.location.href;
528 return doc.URL || doc.location.href;
532 // ====================================================================
537 * The store for all session specific data
543 * 1-1: State ID to State Object
545 History.idToState = History.idToState||{};
549 * 1-1: State String to State ID
551 History.stateToId = History.stateToId||{};
555 * 1-1: State URL to State ID
557 History.urlToId = History.urlToId||{};
560 * History.storedStates
561 * Store the states in an array
563 History.storedStates = History.storedStates||[];
566 * History.savedStates
567 * Saved the states in an array
569 History.savedStates = History.savedStates||[];
572 * History.noramlizeStore()
573 * Noramlize the store by adding necessary values
575 History.normalizeStore = function(){
576 History.store.idToState = History.store.idToState||{};
577 History.store.urlToId = History.store.urlToId||{};
578 History.store.stateToId = History.store.stateToId||{};
583 * Get an object containing the data, title and url of the current state
584 * @param {Boolean} friendly
585 * @param {Boolean} create
586 * @return {Object} State
588 History.getState = function(friendly,create){
590 if ( typeof friendly === 'undefined' ) { friendly = true; }
591 if ( typeof create === 'undefined' ) { create = true; }
594 var State = History.getLastSavedState();
597 if ( !State && create ) {
598 State = History.createStateObject();
603 State = History.cloneObject(State);
604 State.url = State.cleanUrl||State.url;
612 * History.getIdByState(State)
613 * Gets a ID for a State
614 * @param {State} newState
615 * @return {String} id
617 History.getIdByState = function(newState){
620 var id = History.extractId(newState.url),
624 // Find ID via State String
625 str = History.getStateString(newState);
626 if ( typeof History.stateToId[str] !== 'undefined' ) {
627 id = History.stateToId[str];
629 else if ( typeof History.store.stateToId[str] !== 'undefined' ) {
630 id = History.store.stateToId[str];
635 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
636 if ( typeof History.idToState[id] === 'undefined' && typeof History.store.idToState[id] === 'undefined' ) {
641 // Apply the new State to the ID
642 History.stateToId[str] = id;
643 History.idToState[id] = newState;
652 * History.normalizeState(State)
653 * Expands a State Object
654 * @param {object} State
657 History.normalizeState = function(oldState){
659 var newState, dataNotEmpty;
662 if ( !oldState || (typeof oldState !== 'object') ) {
667 if ( typeof oldState.normalized !== 'undefined' ) {
672 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
676 // ----------------------------------------------------------------
680 newState.normalized = true;
681 newState.title = oldState.title||'';
682 newState.url = History.getFullUrl(oldState.url?oldState.url:(History.getLocationHref()));
683 newState.hash = History.getShortUrl(newState.url);
684 newState.data = History.cloneObject(oldState.data);
687 newState.id = History.getIdByState(newState);
689 // ----------------------------------------------------------------
692 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
693 newState.url = newState.cleanUrl;
695 // Check to see if we have more than just a url
696 dataNotEmpty = !History.isEmptyObject(newState.data);
699 if ( (newState.title || dataNotEmpty) && History.options.disableSuid !== true ) {
701 newState.hash = History.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
702 if ( !/\?/.test(newState.hash) ) {
703 newState.hash += '?';
705 newState.hash += '&_suid='+newState.id;
708 // Create the Hashed URL
709 newState.hashedUrl = History.getFullUrl(newState.hash);
711 // ----------------------------------------------------------------
713 // Update the URL if we have a duplicate
714 if ( (History.emulated.pushState || History.bugs.safariPoll) && History.hasUrlDuplicate(newState) ) {
715 newState.url = newState.hashedUrl;
718 // ----------------------------------------------------------------
725 * History.createStateObject(data,title,url)
726 * Creates a object based on the data, title and url state params
727 * @param {object} data
728 * @param {string} title
729 * @param {string} url
732 History.createStateObject = function(data,title,url){
741 State = History.normalizeState(State);
748 * History.getStateById(id)
749 * Get a state by it's UID
752 History.getStateById = function(id){
757 var State = History.idToState[id] || History.store.idToState[id] || undefined;
764 * Get a State's String
765 * @param {State} passedState
767 History.getStateString = function(passedState){
769 var State, cleanedState, str;
772 State = History.normalizeState(passedState);
777 title: passedState.title,
782 str = JSON.stringify(cleanedState);
790 * @param {State} passedState
791 * @return {String} id
793 History.getStateId = function(passedState){
798 State = History.normalizeState(passedState);
808 * History.getHashByState(State)
809 * Creates a Hash for the State Object
810 * @param {State} passedState
811 * @return {String} hash
813 History.getHashByState = function(passedState){
818 State = History.normalizeState(passedState);
828 * History.extractId(url_or_hash)
829 * Get a State ID by it's URL or Hash
830 * @param {string} url_or_hash
831 * @return {string} id
833 History.extractId = function ( url_or_hash ) {
835 var id,parts,url, tmp;
839 // If the URL has a #, use the id from before the #
840 if (url_or_hash.indexOf('#') != -1)
842 tmp = url_or_hash.split("#")[0];
849 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
850 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
851 id = parts ? String(parts[2]||'') : '';
858 * History.isTraditionalAnchor
859 * Checks to see if the url is a traditional anchor or not
860 * @param {String} url_or_hash
863 History.isTraditionalAnchor = function(url_or_hash){
865 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
868 return isTraditional;
872 * History.extractState
873 * Get a State by it's URL or Hash
874 * @param {String} url_or_hash
875 * @return {State|null}
877 History.extractState = function(url_or_hash,create){
879 var State = null, id, url;
880 create = create||false;
883 id = History.extractId(url_or_hash);
885 State = History.getStateById(id);
888 // Fetch SUID returned no State
891 url = History.getFullUrl(url_or_hash);
894 id = History.getIdByUrl(url)||false;
896 State = History.getStateById(id);
900 if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) {
901 State = History.createStateObject(null,null,url);
910 * History.getIdByUrl()
911 * Get a State ID by a State URL
913 History.getIdByUrl = function(url){
915 var id = History.urlToId[url] || History.store.urlToId[url] || undefined;
922 * History.getLastSavedState()
923 * Get an object containing the data, title and url of the current state
924 * @return {Object} State
926 History.getLastSavedState = function(){
927 return History.savedStates[History.savedStates.length-1]||undefined;
931 * History.getLastStoredState()
932 * Get an object containing the data, title and url of the current state
933 * @return {Object} State
935 History.getLastStoredState = function(){
936 return History.storedStates[History.storedStates.length-1]||undefined;
940 * History.hasUrlDuplicate
941 * Checks if a Url will have a url conflict
942 * @param {Object} newState
943 * @return {Boolean} hasDuplicate
945 History.hasUrlDuplicate = function(newState) {
947 var hasDuplicate = false,
951 oldState = History.extractState(newState.url);
954 hasDuplicate = oldState && oldState.id !== newState.id;
963 * @param {Object} newState
964 * @return {Object} newState
966 History.storeState = function(newState){
968 History.urlToId[newState.url] = newState.id;
971 History.storedStates.push(History.cloneObject(newState));
978 * History.isLastSavedState(newState)
979 * Tests to see if the state is the last state
980 * @param {Object} newState
981 * @return {boolean} isLast
983 History.isLastSavedState = function(newState){
986 newId, oldState, oldId;
989 if ( History.savedStates.length ) {
991 oldState = History.getLastSavedState();
995 isLast = (newId === oldId);
1005 * @param {Object} newState
1006 * @return {boolean} changed
1008 History.saveState = function(newState){
1010 if ( History.isLastSavedState(newState) ) {
1015 History.savedStates.push(History.cloneObject(newState));
1022 * History.getStateByIndex()
1023 * Gets a state by the index
1024 * @param {integer} index
1027 History.getStateByIndex = function(index){
1032 if ( typeof index === 'undefined' ) {
1033 // Get the last inserted
1034 State = History.savedStates[History.savedStates.length-1];
1036 else if ( index < 0 ) {
1038 State = History.savedStates[History.savedStates.length+index];
1041 // Get from the beginning
1042 State = History.savedStates[index];
1050 * History.getCurrentIndex()
1051 * Gets the current index
1054 History.getCurrentIndex = function(){
1059 if(History.savedStates.length < 1) {
1063 index = History.savedStates.length-1;
1068 // ====================================================================
1073 * @param {Location=} location
1074 * Gets the current document hash
1075 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1078 History.getHash = function(doc){
1079 var url = History.getLocationHref(doc),
1081 hash = History.getHashByUrl(url);
1086 * History.unescapeHash()
1087 * normalize and Unescape a Hash
1088 * @param {String} hash
1091 History.unescapeHash = function(hash){
1093 var result = History.normalizeHash(hash);
1096 result = decodeURIComponent(result);
1103 * History.normalizeHash()
1104 * normalize a hash across browsers
1107 History.normalizeHash = function(hash){
1109 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1116 * History.setHash(hash)
1117 * Sets the document hash
1118 * @param {string} hash
1121 History.setHash = function(hash,queue){
1126 if ( queue !== false && History.busy() ) {
1127 // Wait + Push to Queue
1128 //History.debug('History.setHash: we must wait', arguments);
1131 callback: History.setHash,
1139 //History.debug('History.setHash: called',hash);
1141 // Make Busy + Continue
1144 // Check if hash is a state
1145 State = History.extractState(hash,true);
1146 if ( State && !History.emulated.pushState ) {
1147 // Hash is a state so skip the setHash
1148 //History.debug('History.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1151 History.pushState(State.data,State.title,State.url,false);
1153 else if ( History.getHash() !== hash ) {
1154 // Hash is a proper hash, so apply it
1156 // Handle browser bugs
1157 if ( History.bugs.setHash ) {
1158 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1160 // Fetch the base page
1161 pageUrl = History.getPageUrl();
1163 // Safari hash apply
1164 History.pushState(null,null,pageUrl+'#'+hash,false);
1167 // Normal hash apply
1168 window.document.location.hash = hash;
1178 * normalize and Escape a Hash
1181 History.escapeHash = function(hash){
1183 var result = History.normalizeHash(hash);
1186 result = window.encodeURIComponent(result);
1189 if ( !History.bugs.hashEscape ) {
1190 // Restore common parts
1192 .replace(/\%21/g,'!')
1193 .replace(/\%26/g,'&')
1194 .replace(/\%3D/g,'=')
1195 .replace(/\%3F/g,'?');
1203 * History.getHashByUrl(url)
1204 * Extracts the Hash from a URL
1205 * @param {string} url
1206 * @return {string} url
1208 History.getHashByUrl = function(url){
1210 var hash = String(url)
1211 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1215 hash = History.unescapeHash(hash);
1222 * History.setTitle(title)
1223 * Applies the title to the document
1224 * @param {State} newState
1227 History.setTitle = function(newState){
1229 var title = newState.title,
1234 firstState = History.getStateByIndex(0);
1235 if ( firstState && firstState.url === newState.url ) {
1236 title = firstState.title||History.options.initialTitle;
1242 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1244 catch ( Exception ) { }
1245 window.document.title = title;
1252 // ====================================================================
1257 * The list of queues to use
1258 * First In, First Out
1260 History.queues = [];
1263 * History.busy(value)
1264 * @param {boolean} value [optional]
1265 * @return {boolean} busy
1267 History.busy = function(value){
1269 if ( typeof value !== 'undefined' ) {
1270 //History.debug('History.busy: changing ['+(History.busy.flag||false)+'] to ['+(value||false)+']', History.queues.length);
1271 History.busy.flag = value;
1274 else if ( typeof History.busy.flag === 'undefined' ) {
1275 History.busy.flag = false;
1279 if ( !History.busy.flag ) {
1280 // Execute the next item in the queue
1281 window.clearTimeout(History.busy.timeout);
1282 var fireNext = function(){
1284 if ( History.busy.flag ) return;
1285 for ( i=History.queues.length-1; i >= 0; --i ) {
1286 queue = History.queues[i];
1287 if ( queue.length === 0 ) continue;
1288 item = queue.shift();
1289 History.fireQueueItem(item);
1290 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1293 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1297 return History.busy.flag;
1303 History.busy.flag = false;
1306 * History.fireQueueItem(item)
1308 * @param {Object} item
1309 * @return {Mixed} result
1311 History.fireQueueItem = function(item){
1312 return item.callback.apply(item.scope||History,item.args||[]);
1316 * History.pushQueue(callback,args)
1317 * Add an item to the queue
1318 * @param {Object} item [scope,callback,args,queue]
1320 History.pushQueue = function(item){
1321 // Prepare the queue
1322 History.queues[item.queue||0] = History.queues[item.queue||0]||[];
1325 History.queues[item.queue||0].push(item);
1332 * History.queue (item,queue), (func,queue), (func), (item)
1333 * Either firs the item now if not busy, or adds it to the queue
1335 History.queue = function(item,queue){
1337 if ( typeof item === 'function' ) {
1342 if ( typeof queue !== 'undefined' ) {
1347 if ( History.busy() ) {
1348 History.pushQueue(item);
1350 History.fireQueueItem(item);
1358 * History.clearQueue()
1361 History.clearQueue = function(){
1362 History.busy.flag = false;
1363 History.queues = [];
1368 // ====================================================================
1372 * History.stateChanged
1373 * States whether or not the state has changed since the last double check was initialised
1375 History.stateChanged = false;
1378 * History.doubleChecker
1379 * Contains the timeout used for the double checks
1381 History.doubleChecker = false;
1384 * History.doubleCheckComplete()
1385 * Complete a double check
1388 History.doubleCheckComplete = function(){
1390 History.stateChanged = true;
1393 History.doubleCheckClear();
1400 * History.doubleCheckClear()
1401 * Clear a double check
1404 History.doubleCheckClear = function(){
1406 if ( History.doubleChecker ) {
1407 window.clearTimeout(History.doubleChecker);
1408 History.doubleChecker = false;
1416 * History.doubleCheck()
1417 * Create a double check
1420 History.doubleCheck = function(tryAgain){
1422 History.stateChanged = false;
1423 History.doubleCheckClear();
1425 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1426 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1427 if ( History.bugs.ieDoubleCheck ) {
1429 History.doubleChecker = window.setTimeout(
1431 History.doubleCheckClear();
1432 if ( !History.stateChanged ) {
1433 //History.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1439 History.options.doubleCheckInterval
1448 // ====================================================================
1452 * History.safariStatePoll()
1453 * Poll the current state
1456 History.safariStatePoll = function(){
1459 // Get the Last State which has the new URL
1461 urlState = History.extractState(History.getLocationHref()),
1464 // Check for a difference
1465 if ( !History.isLastSavedState(urlState) ) {
1466 newState = urlState;
1472 // Check if we have a state with that url
1475 //History.debug('History.safariStatePoll: new');
1476 newState = History.createStateObject();
1479 // Apply the New State
1480 //History.debug('History.safariStatePoll: trigger');
1481 History.Adapter.trigger(window,'popstate');
1488 // ====================================================================
1492 * History.back(queue)
1493 * Send the browser history back one item
1494 * @param {Integer} queue [optional]
1496 History.back = function(queue){
1497 //History.debug('History.back: called', arguments);
1500 if ( queue !== false && History.busy() ) {
1501 // Wait + Push to Queue
1502 //History.debug('History.back: we must wait', arguments);
1505 callback: History.back,
1512 // Make Busy + Continue
1515 // Fix certain browser bugs that prevent the state from changing
1516 History.doubleCheck(function(){
1517 History.back(false);
1528 * History.forward(queue)
1529 * Send the browser history forward one item
1530 * @param {Integer} queue [optional]
1532 History.forward = function(queue){
1533 //History.debug('History.forward: called', arguments);
1536 if ( queue !== false && History.busy() ) {
1537 // Wait + Push to Queue
1538 //History.debug('History.forward: we must wait', arguments);
1541 callback: History.forward,
1548 // Make Busy + Continue
1551 // Fix certain browser bugs that prevent the state from changing
1552 History.doubleCheck(function(){
1553 History.forward(false);
1559 // End forward closure
1564 * History.go(index,queue)
1565 * Send the browser history back or forward index times
1566 * @param {Integer} queue [optional]
1568 History.go = function(index,queue){
1569 //History.debug('History.go: called', arguments);
1577 for ( i=1; i<=index; ++i ) {
1578 History.forward(queue);
1581 else if ( index < 0 ) {
1583 for ( i=-1; i>=index; --i ) {
1584 History.back(queue);
1588 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1596 // ====================================================================
1597 // HTML5 State Support
1599 // Non-Native pushState Implementation
1600 if ( History.emulated.pushState ) {
1602 * Provide Skeleton for HTML4 Browsers
1606 var emptyFunction = function(){};
1607 History.pushState = History.pushState||emptyFunction;
1608 History.replaceState = History.replaceState||emptyFunction;
1609 } // History.emulated.pushState
1611 // Native pushState Implementation
1614 * Use native HTML5 History API Implementation
1618 * History.onPopState(event,extra)
1619 * Refresh the Current State
1621 History.onPopState = function(event,extra){
1623 var stateId = false, newState = false, currentHash, currentState;
1625 // Reset the double check
1626 History.doubleCheckComplete();
1628 // Check for a Hash, and handle apporiatly
1629 currentHash = History.getHash();
1630 if ( currentHash ) {
1632 currentState = History.extractState(currentHash||History.getLocationHref(),true);
1633 if ( currentState ) {
1634 // We were able to parse it, it must be a State!
1635 // Let's forward to replaceState
1636 //History.debug('History.onPopState: state anchor', currentHash, currentState);
1637 History.replaceState(currentState.data, currentState.title, currentState.url, false);
1640 // Traditional Anchor
1641 //History.debug('History.onPopState: traditional anchor', currentHash);
1642 History.Adapter.trigger(window,'anchorchange');
1643 History.busy(false);
1646 // We don't care for hashes
1647 History.expectedStateId = false;
1652 stateId = History.Adapter.extractEventData('state',event,extra) || false;
1656 // Vanilla: Back/forward button was used
1657 newState = History.getStateById(stateId);
1659 else if ( History.expectedStateId ) {
1660 // Vanilla: A new state was pushed, and popstate was called manually
1661 newState = History.getStateById(History.expectedStateId);
1665 newState = History.extractState(History.getLocationHref());
1668 // The State did not exist in our store
1670 // Regenerate the State
1671 newState = History.createStateObject(null,null,History.getLocationHref());
1675 History.expectedStateId = false;
1677 // Check if we are the same state
1678 if ( History.isLastSavedState(newState) ) {
1679 // There has been no change (just the page's hash has finally propagated)
1680 //History.debug('History.onPopState: no change', newState, History.savedStates);
1681 History.busy(false);
1686 History.storeState(newState);
1687 History.saveState(newState);
1689 // Force update of the title
1690 History.setTitle(newState);
1693 History.Adapter.trigger(window,'statechange');
1694 History.busy(false);
1699 History.Adapter.bind(window,'popstate',History.onPopState);
1702 * History.pushState(data,title,url)
1703 * Add a new State to the history object, become it, and trigger onpopstate
1704 * We have to trigger for HTML4 compatibility
1705 * @param {object} data
1706 * @param {string} title
1707 * @param {string} url
1710 History.pushState = function(data,title,url,queue){
1711 //History.debug('History.pushState: called', arguments);
1714 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1715 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1719 if ( queue !== false && History.busy() ) {
1720 // Wait + Push to Queue
1721 //History.debug('History.pushState: we must wait', arguments);
1724 callback: History.pushState,
1731 // Make Busy + Continue
1734 // Create the newState
1735 var newState = History.createStateObject(data,title,url);
1738 if ( History.isLastSavedState(newState) ) {
1739 // Won't be a change
1740 History.busy(false);
1743 // Store the newState
1744 History.storeState(newState);
1745 History.expectedStateId = newState.id;
1747 // Push the newState
1748 history.pushState(newState.id,newState.title,newState.url);
1751 History.Adapter.trigger(window,'popstate');
1754 // End pushState closure
1759 * History.replaceState(data,title,url)
1760 * Replace the State and trigger onpopstate
1761 * We have to trigger for HTML4 compatibility
1762 * @param {object} data
1763 * @param {string} title
1764 * @param {string} url
1767 History.replaceState = function(data,title,url,queue){
1768 //History.debug('History.replaceState: called', arguments);
1771 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1772 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1776 if ( queue !== false && History.busy() ) {
1777 // Wait + Push to Queue
1778 //History.debug('History.replaceState: we must wait', arguments);
1781 callback: History.replaceState,
1788 // Make Busy + Continue
1791 // Create the newState
1792 var newState = History.createStateObject(data,title,url);
1795 if ( History.isLastSavedState(newState) ) {
1796 // Won't be a change
1797 History.busy(false);
1800 // Store the newState
1801 History.storeState(newState);
1802 History.expectedStateId = newState.id;
1804 // Push the newState
1805 history.replaceState(newState.id,newState.title,newState.url);
1808 History.Adapter.trigger(window,'popstate');
1811 // End replaceState closure
1815 } // !History.emulated.pushState
1818 // ====================================================================
1824 if ( sessionStorage ) {
1827 History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
1834 History.normalizeStore();
1839 History.normalizeStore();
1843 * Clear Intervals on exit to prevent memory leaks
1845 History.Adapter.bind(window,"unload",History.clearAllIntervals);
1848 * Create the initial State
1850 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
1853 * Bind for Saving Store
1855 if ( sessionStorage ) {
1856 // When the page is closed
1857 History.onUnload = function(){
1859 var currentStore, item, currentStoreString;
1863 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1870 currentStore.idToState = currentStore.idToState || {};
1871 currentStore.urlToId = currentStore.urlToId || {};
1872 currentStore.stateToId = currentStore.stateToId || {};
1875 for ( item in History.idToState ) {
1876 if ( !History.idToState.hasOwnProperty(item) ) {
1879 currentStore.idToState[item] = History.idToState[item];
1881 for ( item in History.urlToId ) {
1882 if ( !History.urlToId.hasOwnProperty(item) ) {
1885 currentStore.urlToId[item] = History.urlToId[item];
1887 for ( item in History.stateToId ) {
1888 if ( !History.stateToId.hasOwnProperty(item) ) {
1891 currentStore.stateToId[item] = History.stateToId[item];
1895 History.store = currentStore;
1896 History.normalizeStore();
1898 // In Safari, going into Private Browsing mode causes the
1899 // Session Storage object to still exist but if you try and use
1900 // or set any property/function of it it throws the exception
1901 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1902 // add something to storage that exceeded the quota." infinitely
1904 currentStoreString = JSON.stringify(currentStore);
1907 sessionStorage.setItem('History.store', currentStoreString);
1910 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1911 if (sessionStorage.length) {
1912 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1913 // removing/resetting the storage can work.
1914 sessionStorage.removeItem('History.store');
1915 sessionStorage.setItem('History.store', currentStoreString);
1917 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1925 // For Internet Explorer
1926 History.intervalList.push(setInterval(History.onUnload,History.options.storeInterval));
1928 // For Other Browsers
1929 History.Adapter.bind(window,'beforeunload',History.onUnload);
1930 History.Adapter.bind(window,'unload',History.onUnload);
1932 // Both are enabled for consistency
1935 // Non-Native pushState Implementation
1936 if ( !History.emulated.pushState ) {
1937 // Be aware, the following is only for native pushState implementations
1938 // If you are wanting to include something for all browsers
1939 // Then include it above this if block
1944 if ( History.bugs.safariPoll ) {
1945 History.intervalList.push(setInterval(History.safariStatePoll, History.options.safariPollInterval));
1949 * Ensure Cross Browser Compatibility
1951 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1953 * Fix Safari HashChange Issue
1957 History.Adapter.bind(window,'hashchange',function(){
1958 History.Adapter.trigger(window,'popstate');
1962 if ( History.getHash() ) {
1963 History.Adapter.onDomLoad(function(){
1964 History.Adapter.trigger(window,'hashchange');
1969 } // !History.emulated.pushState
1972 }; // History.initCore
1974 // Try to Initialise History
1975 if (!History.options || !History.options.delayInit) {