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 // Initialise History
117 init : function(options){
119 initialTitle : window.document.title,
121 Roo.apply(this,options)
123 // Check Load Status of Adapter
124 //if ( typeof this.Adapter === 'undefined' ) {
128 // Check Load Status of Core
129 if ( typeof this.initCore !== 'undefined' ) {
133 // Check Load Status of HTML4 Support
134 if ( typeof this.initHtml4 !== 'undefined' ) {
141 * Is History enabled?
143 this.enabled = !this.emulated.pushState;
152 // ========================================================================
156 initCore : function(options){
158 this.intervalList = [];
162 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
163 this.sessionStorage.setItem('TEST', '1');
164 this.sessionStorage.removeItem('TEST');
166 this.sessionStorage = false;
170 if ( typeof this.initCore.initialized !== 'undefined' ) {
175 this.initCore.initialized = true;
183 * History.clearAllIntervals
184 * Clears all setInterval instances.
186 clearAllIntervals: function()
188 var i, il = this.intervalList;
189 if (typeof il !== "undefined" && il !== null) {
190 for (i = 0; i < il.length; i++) {
191 clearInterval(il[i]);
193 this.intervalList = null;
198 // ====================================================================
202 * History.debugLog(message,...)
203 * Logs the passed arguments if debug enabled
205 debugLog : function()
207 if ( (this.debug||false) ) {
208 Roo.log.apply(History,arguments);
214 // ====================================================================
218 * History.getInternetExplorerMajorVersion()
219 * Get's the major version of Internet Explorer
221 * @license Public Domain
222 * @author Benjamin Arthur Lupton <contact@balupton.com>
223 * @author James Padolsey <https://gist.github.com/527683>
225 getInternetExplorerMajorVersion = function(){
226 var result = this.getInternetExplorerMajorVersion.cached =
227 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
228 ? this.getInternetExplorerMajorVersion.cached
231 div = window.document.createElement('div'),
232 all = div.getElementsByTagName('i');
233 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
234 return (v > 4) ? v : false;
241 * isInternetExplorer()
242 * Are we using Internet Explorer?
244 * @license Public Domain
245 * @author Benjamin Arthur Lupton <contact@balupton.com>
247 isInternetExplorer = function(){
249 this.isInternetExplorer.cached =
250 (typeof this.isInternetExplorer.cached !== 'undefined')
251 ? this.isInternetExplorer.cached
252 : Boolean(this.getInternetExplorerMajorVersion())
258 * Which features require emulating?
266 initEmulated : function()
270 if (this.html4Mode) {
276 this.emulated.pushState = !Boolean(
277 window.history && window.history.pushState && window.history.replaceState
279 (/ 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) */
280 || (/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 */
283 this.emulated.hashChange = Boolean(
284 !(('onhashchange' in window) || ('onhashchange' in window.document))
286 (History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8)
293 * Is History enabled?
295 History.enabled = !History.emulated.pushState;
300 * History.isEmptyObject(obj)
301 * Checks to see if the Object is Empty
302 * @param {Object} obj
305 History.isEmptyObject = function(obj) {
306 for ( var name in obj ) {
307 if ( obj.hasOwnProperty(name) ) {
315 * History.cloneObject(obj)
316 * Clones a object and eliminate all references to the original contexts
317 * @param {Object} obj
320 History.cloneObject = function(obj) {
323 hash = JSON.stringify(obj);
324 newObj = JSON.parse(hash);
333 // ====================================================================
337 * History.getRootUrl()
338 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
339 * @return {String} rootUrl
341 History.getRootUrl = function(){
343 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
344 if ( window.document.location.port||false ) {
345 rootUrl += ':'+window.document.location.port;
354 * History.getBaseHref()
355 * Fetches the `href` attribute of the `<base href="...">` element if it exists
356 * @return {String} baseHref
358 History.getBaseHref = function(){
361 baseElements = window.document.getElementsByTagName('base'),
365 // Test for Base Element
366 if ( baseElements.length === 1 ) {
367 // Prepare for Base Element
368 baseElement = baseElements[0];
369 baseHref = baseElement.href.replace(/[^\/]+$/,'');
372 // Adjust trailing slash
373 baseHref = baseHref.replace(/\/+$/,'');
374 if ( baseHref ) baseHref += '/';
381 * History.getBaseUrl()
382 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
383 * @return {String} baseUrl
385 History.getBaseUrl = function(){
387 var baseUrl = History.getBaseHref()||History.getBasePageUrl()||History.getRootUrl();
394 * History.getPageUrl()
395 * Fetches the URL of the current page
396 * @return {String} pageUrl
398 History.getPageUrl = function(){
401 State = History.getState(false,false),
402 stateUrl = (State||{}).url||History.getLocationHref(),
406 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
407 return (/\./).test(part) ? part : part+'/';
415 * History.getBasePageUrl()
416 * Fetches the Url of the directory of the current page
417 * @return {String} basePageUrl
419 History.getBasePageUrl = function(){
421 var basePageUrl = (History.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
422 return (/[^\/]$/).test(part) ? '' : part;
423 }).replace(/\/+$/,'')+'/';
430 * History.getFullUrl(url)
431 * Ensures that we have an absolute URL and not a relative URL
432 * @param {string} url
433 * @param {Boolean} allowBaseHref
434 * @return {string} fullUrl
436 History.getFullUrl = function(url,allowBaseHref){
438 var fullUrl = url, firstChar = url.substring(0,1);
439 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
442 if ( /[a-z]+\:\/\//.test(url) ) {
445 else if ( firstChar === '/' ) {
447 fullUrl = History.getRootUrl()+url.replace(/^\/+/,'');
449 else if ( firstChar === '#' ) {
451 fullUrl = History.getPageUrl().replace(/#.*/,'')+url;
453 else if ( firstChar === '?' ) {
455 fullUrl = History.getPageUrl().replace(/[\?#].*/,'')+url;
459 if ( allowBaseHref ) {
460 fullUrl = History.getBaseUrl()+url.replace(/^(\.\/)+/,'');
462 fullUrl = History.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
464 // We have an if condition above as we do not want hashes
465 // which are relative to the baseHref in our URLs
466 // as if the baseHref changes, then all our bookmarks
467 // would now point to different locations
468 // whereas the basePageUrl will always stay the same
472 return fullUrl.replace(/\#$/,'');
476 * History.getShortUrl(url)
477 * Ensures that we have a relative URL and not a absolute URL
478 * @param {string} url
479 * @return {string} url
481 History.getShortUrl = function(url){
483 var shortUrl = url, baseUrl = History.getBaseUrl(), rootUrl = History.getRootUrl();
486 if ( History.emulated.pushState ) {
487 // We are in a if statement as when pushState is not emulated
488 // The actual url these short urls are relative to can change
489 // So within the same session, we the url may end up somewhere different
490 shortUrl = shortUrl.replace(baseUrl,'');
494 shortUrl = shortUrl.replace(rootUrl,'/');
496 // Ensure we can still detect it as a state
497 if ( History.isTraditionalAnchor(shortUrl) ) {
498 shortUrl = './'+shortUrl;
502 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
509 * History.getLocationHref(document)
510 * Returns a normalized version of document.location.href
511 * accounting for browser inconsistencies, etc.
513 * This URL will be URI-encoded and will include the hash
515 * @param {object} document
516 * @return {string} url
518 History.getLocationHref = function(doc) {
519 doc = doc || window.document;
521 // most of the time, this will be true
522 if (doc.URL === doc.location.href)
523 return doc.location.href;
525 // some versions of webkit URI-decode document.location.href
526 // but they leave document.URL in an encoded state
527 if (doc.location.href === decodeURIComponent(doc.URL))
530 // FF 3.6 only updates document.URL when a page is reloaded
531 // document.location.href is updated correctly
532 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
533 return doc.location.href;
535 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
536 return doc.location.href;
538 return doc.URL || doc.location.href;
542 // ====================================================================
547 * The store for all session specific data
553 * 1-1: State ID to State Object
555 History.idToState = History.idToState||{};
559 * 1-1: State String to State ID
561 History.stateToId = History.stateToId||{};
565 * 1-1: State URL to State ID
567 History.urlToId = History.urlToId||{};
570 * History.storedStates
571 * Store the states in an array
573 History.storedStates = History.storedStates||[];
576 * History.savedStates
577 * Saved the states in an array
579 History.savedStates = History.savedStates||[];
582 * History.noramlizeStore()
583 * Noramlize the store by adding necessary values
585 History.normalizeStore = function(){
586 History.store.idToState = History.store.idToState||{};
587 History.store.urlToId = History.store.urlToId||{};
588 History.store.stateToId = History.store.stateToId||{};
593 * Get an object containing the data, title and url of the current state
594 * @param {Boolean} friendly
595 * @param {Boolean} create
596 * @return {Object} State
598 History.getState = function(friendly,create){
600 if ( typeof friendly === 'undefined' ) { friendly = true; }
601 if ( typeof create === 'undefined' ) { create = true; }
604 var State = History.getLastSavedState();
607 if ( !State && create ) {
608 State = History.createStateObject();
613 State = History.cloneObject(State);
614 State.url = State.cleanUrl||State.url;
622 * History.getIdByState(State)
623 * Gets a ID for a State
624 * @param {State} newState
625 * @return {String} id
627 History.getIdByState = function(newState){
630 var id = History.extractId(newState.url),
634 // Find ID via State String
635 str = History.getStateString(newState);
636 if ( typeof History.stateToId[str] !== 'undefined' ) {
637 id = History.stateToId[str];
639 else if ( typeof History.store.stateToId[str] !== 'undefined' ) {
640 id = History.store.stateToId[str];
645 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
646 if ( typeof History.idToState[id] === 'undefined' && typeof History.store.idToState[id] === 'undefined' ) {
651 // Apply the new State to the ID
652 History.stateToId[str] = id;
653 History.idToState[id] = newState;
662 * History.normalizeState(State)
663 * Expands a State Object
664 * @param {object} State
667 History.normalizeState = function(oldState){
669 var newState, dataNotEmpty;
672 if ( !oldState || (typeof oldState !== 'object') ) {
677 if ( typeof oldState.normalized !== 'undefined' ) {
682 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
686 // ----------------------------------------------------------------
690 newState.normalized = true;
691 newState.title = oldState.title||'';
692 newState.url = History.getFullUrl(oldState.url?oldState.url:(History.getLocationHref()));
693 newState.hash = History.getShortUrl(newState.url);
694 newState.data = History.cloneObject(oldState.data);
697 newState.id = History.getIdByState(newState);
699 // ----------------------------------------------------------------
702 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
703 newState.url = newState.cleanUrl;
705 // Check to see if we have more than just a url
706 dataNotEmpty = !History.isEmptyObject(newState.data);
709 if ( (newState.title || dataNotEmpty) && History.options.disableSuid !== true ) {
711 newState.hash = History.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
712 if ( !/\?/.test(newState.hash) ) {
713 newState.hash += '?';
715 newState.hash += '&_suid='+newState.id;
718 // Create the Hashed URL
719 newState.hashedUrl = History.getFullUrl(newState.hash);
721 // ----------------------------------------------------------------
723 // Update the URL if we have a duplicate
724 if ( (History.emulated.pushState || History.bugs.safariPoll) && History.hasUrlDuplicate(newState) ) {
725 newState.url = newState.hashedUrl;
728 // ----------------------------------------------------------------
735 * History.createStateObject(data,title,url)
736 * Creates a object based on the data, title and url state params
737 * @param {object} data
738 * @param {string} title
739 * @param {string} url
742 History.createStateObject = function(data,title,url){
751 State = History.normalizeState(State);
758 * History.getStateById(id)
759 * Get a state by it's UID
762 History.getStateById = function(id){
767 var State = History.idToState[id] || History.store.idToState[id] || undefined;
774 * Get a State's String
775 * @param {State} passedState
777 History.getStateString = function(passedState){
779 var State, cleanedState, str;
782 State = History.normalizeState(passedState);
787 title: passedState.title,
792 str = JSON.stringify(cleanedState);
800 * @param {State} passedState
801 * @return {String} id
803 History.getStateId = function(passedState){
808 State = History.normalizeState(passedState);
818 * History.getHashByState(State)
819 * Creates a Hash for the State Object
820 * @param {State} passedState
821 * @return {String} hash
823 History.getHashByState = function(passedState){
828 State = History.normalizeState(passedState);
838 * History.extractId(url_or_hash)
839 * Get a State ID by it's URL or Hash
840 * @param {string} url_or_hash
841 * @return {string} id
843 History.extractId = function ( url_or_hash ) {
845 var id,parts,url, tmp;
849 // If the URL has a #, use the id from before the #
850 if (url_or_hash.indexOf('#') != -1)
852 tmp = url_or_hash.split("#")[0];
859 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
860 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
861 id = parts ? String(parts[2]||'') : '';
868 * History.isTraditionalAnchor
869 * Checks to see if the url is a traditional anchor or not
870 * @param {String} url_or_hash
873 History.isTraditionalAnchor = function(url_or_hash){
875 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
878 return isTraditional;
882 * History.extractState
883 * Get a State by it's URL or Hash
884 * @param {String} url_or_hash
885 * @return {State|null}
887 History.extractState = function(url_or_hash,create){
889 var State = null, id, url;
890 create = create||false;
893 id = History.extractId(url_or_hash);
895 State = History.getStateById(id);
898 // Fetch SUID returned no State
901 url = History.getFullUrl(url_or_hash);
904 id = History.getIdByUrl(url)||false;
906 State = History.getStateById(id);
910 if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) {
911 State = History.createStateObject(null,null,url);
920 * History.getIdByUrl()
921 * Get a State ID by a State URL
923 History.getIdByUrl = function(url){
925 var id = History.urlToId[url] || History.store.urlToId[url] || undefined;
932 * History.getLastSavedState()
933 * Get an object containing the data, title and url of the current state
934 * @return {Object} State
936 History.getLastSavedState = function(){
937 return History.savedStates[History.savedStates.length-1]||undefined;
941 * History.getLastStoredState()
942 * Get an object containing the data, title and url of the current state
943 * @return {Object} State
945 History.getLastStoredState = function(){
946 return History.storedStates[History.storedStates.length-1]||undefined;
950 * History.hasUrlDuplicate
951 * Checks if a Url will have a url conflict
952 * @param {Object} newState
953 * @return {Boolean} hasDuplicate
955 History.hasUrlDuplicate = function(newState) {
957 var hasDuplicate = false,
961 oldState = History.extractState(newState.url);
964 hasDuplicate = oldState && oldState.id !== newState.id;
973 * @param {Object} newState
974 * @return {Object} newState
976 History.storeState = function(newState){
978 History.urlToId[newState.url] = newState.id;
981 History.storedStates.push(History.cloneObject(newState));
988 * History.isLastSavedState(newState)
989 * Tests to see if the state is the last state
990 * @param {Object} newState
991 * @return {boolean} isLast
993 History.isLastSavedState = function(newState){
996 newId, oldState, oldId;
999 if ( History.savedStates.length ) {
1000 newId = newState.id;
1001 oldState = History.getLastSavedState();
1002 oldId = oldState.id;
1005 isLast = (newId === oldId);
1015 * @param {Object} newState
1016 * @return {boolean} changed
1018 History.saveState = function(newState){
1020 if ( History.isLastSavedState(newState) ) {
1025 History.savedStates.push(History.cloneObject(newState));
1032 * History.getStateByIndex()
1033 * Gets a state by the index
1034 * @param {integer} index
1037 History.getStateByIndex = function(index){
1042 if ( typeof index === 'undefined' ) {
1043 // Get the last inserted
1044 State = History.savedStates[History.savedStates.length-1];
1046 else if ( index < 0 ) {
1048 State = History.savedStates[History.savedStates.length+index];
1051 // Get from the beginning
1052 State = History.savedStates[index];
1060 * History.getCurrentIndex()
1061 * Gets the current index
1064 History.getCurrentIndex = function(){
1069 if(History.savedStates.length < 1) {
1073 index = History.savedStates.length-1;
1078 // ====================================================================
1083 * @param {Location=} location
1084 * Gets the current document hash
1085 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1088 History.getHash = function(doc){
1089 var url = History.getLocationHref(doc),
1091 hash = History.getHashByUrl(url);
1096 * History.unescapeHash()
1097 * normalize and Unescape a Hash
1098 * @param {String} hash
1101 History.unescapeHash = function(hash){
1103 var result = History.normalizeHash(hash);
1106 result = decodeURIComponent(result);
1113 * History.normalizeHash()
1114 * normalize a hash across browsers
1117 History.normalizeHash = function(hash){
1119 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1126 * History.setHash(hash)
1127 * Sets the document hash
1128 * @param {string} hash
1131 History.setHash = function(hash,queue){
1136 if ( queue !== false && History.busy() ) {
1137 // Wait + Push to Queue
1138 //History.debug('History.setHash: we must wait', arguments);
1141 callback: History.setHash,
1149 //History.debug('History.setHash: called',hash);
1151 // Make Busy + Continue
1154 // Check if hash is a state
1155 State = History.extractState(hash,true);
1156 if ( State && !History.emulated.pushState ) {
1157 // Hash is a state so skip the setHash
1158 //History.debug('History.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1161 History.pushState(State.data,State.title,State.url,false);
1163 else if ( History.getHash() !== hash ) {
1164 // Hash is a proper hash, so apply it
1166 // Handle browser bugs
1167 if ( History.bugs.setHash ) {
1168 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1170 // Fetch the base page
1171 pageUrl = History.getPageUrl();
1173 // Safari hash apply
1174 History.pushState(null,null,pageUrl+'#'+hash,false);
1177 // Normal hash apply
1178 window.document.location.hash = hash;
1188 * normalize and Escape a Hash
1191 History.escapeHash = function(hash){
1193 var result = History.normalizeHash(hash);
1196 result = window.encodeURIComponent(result);
1199 if ( !History.bugs.hashEscape ) {
1200 // Restore common parts
1202 .replace(/\%21/g,'!')
1203 .replace(/\%26/g,'&')
1204 .replace(/\%3D/g,'=')
1205 .replace(/\%3F/g,'?');
1213 * History.getHashByUrl(url)
1214 * Extracts the Hash from a URL
1215 * @param {string} url
1216 * @return {string} url
1218 History.getHashByUrl = function(url){
1220 var hash = String(url)
1221 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1225 hash = History.unescapeHash(hash);
1232 * History.setTitle(title)
1233 * Applies the title to the document
1234 * @param {State} newState
1237 History.setTitle = function(newState){
1239 var title = newState.title,
1244 firstState = History.getStateByIndex(0);
1245 if ( firstState && firstState.url === newState.url ) {
1246 title = firstState.title||History.options.initialTitle;
1252 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1254 catch ( Exception ) { }
1255 window.document.title = title;
1262 // ====================================================================
1267 * The list of queues to use
1268 * First In, First Out
1270 History.queues = [];
1273 * History.busy(value)
1274 * @param {boolean} value [optional]
1275 * @return {boolean} busy
1277 History.busy = function(value){
1279 if ( typeof value !== 'undefined' ) {
1280 //History.debug('History.busy: changing ['+(History.busy.flag||false)+'] to ['+(value||false)+']', History.queues.length);
1281 History.busy.flag = value;
1284 else if ( typeof History.busy.flag === 'undefined' ) {
1285 History.busy.flag = false;
1289 if ( !History.busy.flag ) {
1290 // Execute the next item in the queue
1291 window.clearTimeout(History.busy.timeout);
1292 var fireNext = function(){
1294 if ( History.busy.flag ) return;
1295 for ( i=History.queues.length-1; i >= 0; --i ) {
1296 queue = History.queues[i];
1297 if ( queue.length === 0 ) continue;
1298 item = queue.shift();
1299 History.fireQueueItem(item);
1300 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1303 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1307 return History.busy.flag;
1313 History.busy.flag = false;
1316 * History.fireQueueItem(item)
1318 * @param {Object} item
1319 * @return {Mixed} result
1321 History.fireQueueItem = function(item){
1322 return item.callback.apply(item.scope||History,item.args||[]);
1326 * History.pushQueue(callback,args)
1327 * Add an item to the queue
1328 * @param {Object} item [scope,callback,args,queue]
1330 History.pushQueue = function(item){
1331 // Prepare the queue
1332 History.queues[item.queue||0] = History.queues[item.queue||0]||[];
1335 History.queues[item.queue||0].push(item);
1342 * History.queue (item,queue), (func,queue), (func), (item)
1343 * Either firs the item now if not busy, or adds it to the queue
1345 History.queue = function(item,queue){
1347 if ( typeof item === 'function' ) {
1352 if ( typeof queue !== 'undefined' ) {
1357 if ( History.busy() ) {
1358 History.pushQueue(item);
1360 History.fireQueueItem(item);
1368 * History.clearQueue()
1371 History.clearQueue = function(){
1372 History.busy.flag = false;
1373 History.queues = [];
1378 // ====================================================================
1382 * History.stateChanged
1383 * States whether or not the state has changed since the last double check was initialised
1385 History.stateChanged = false;
1388 * History.doubleChecker
1389 * Contains the timeout used for the double checks
1391 History.doubleChecker = false;
1394 * History.doubleCheckComplete()
1395 * Complete a double check
1398 History.doubleCheckComplete = function(){
1400 History.stateChanged = true;
1403 History.doubleCheckClear();
1410 * History.doubleCheckClear()
1411 * Clear a double check
1414 History.doubleCheckClear = function(){
1416 if ( History.doubleChecker ) {
1417 window.clearTimeout(History.doubleChecker);
1418 History.doubleChecker = false;
1426 * History.doubleCheck()
1427 * Create a double check
1430 History.doubleCheck = function(tryAgain){
1432 History.stateChanged = false;
1433 History.doubleCheckClear();
1435 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1436 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1437 if ( History.bugs.ieDoubleCheck ) {
1439 History.doubleChecker = window.setTimeout(
1441 History.doubleCheckClear();
1442 if ( !History.stateChanged ) {
1443 //History.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1449 History.options.doubleCheckInterval
1458 // ====================================================================
1462 * History.safariStatePoll()
1463 * Poll the current state
1466 History.safariStatePoll = function(){
1469 // Get the Last State which has the new URL
1471 urlState = History.extractState(History.getLocationHref()),
1474 // Check for a difference
1475 if ( !History.isLastSavedState(urlState) ) {
1476 newState = urlState;
1482 // Check if we have a state with that url
1485 //History.debug('History.safariStatePoll: new');
1486 newState = History.createStateObject();
1489 // Apply the New State
1490 //History.debug('History.safariStatePoll: trigger');
1491 History.Adapter.trigger(window,'popstate');
1498 // ====================================================================
1502 * History.back(queue)
1503 * Send the browser history back one item
1504 * @param {Integer} queue [optional]
1506 History.back = function(queue){
1507 //History.debug('History.back: called', arguments);
1510 if ( queue !== false && History.busy() ) {
1511 // Wait + Push to Queue
1512 //History.debug('History.back: we must wait', arguments);
1515 callback: History.back,
1522 // Make Busy + Continue
1525 // Fix certain browser bugs that prevent the state from changing
1526 History.doubleCheck(function(){
1527 History.back(false);
1538 * History.forward(queue)
1539 * Send the browser history forward one item
1540 * @param {Integer} queue [optional]
1542 History.forward = function(queue){
1543 //History.debug('History.forward: called', arguments);
1546 if ( queue !== false && History.busy() ) {
1547 // Wait + Push to Queue
1548 //History.debug('History.forward: we must wait', arguments);
1551 callback: History.forward,
1558 // Make Busy + Continue
1561 // Fix certain browser bugs that prevent the state from changing
1562 History.doubleCheck(function(){
1563 History.forward(false);
1569 // End forward closure
1574 * History.go(index,queue)
1575 * Send the browser history back or forward index times
1576 * @param {Integer} queue [optional]
1578 History.go = function(index,queue){
1579 //History.debug('History.go: called', arguments);
1587 for ( i=1; i<=index; ++i ) {
1588 History.forward(queue);
1591 else if ( index < 0 ) {
1593 for ( i=-1; i>=index; --i ) {
1594 History.back(queue);
1598 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1606 // ====================================================================
1607 // HTML5 State Support
1609 // Non-Native pushState Implementation
1610 if ( History.emulated.pushState ) {
1612 * Provide Skeleton for HTML4 Browsers
1616 var emptyFunction = function(){};
1617 History.pushState = History.pushState||emptyFunction;
1618 History.replaceState = History.replaceState||emptyFunction;
1619 } // History.emulated.pushState
1621 // Native pushState Implementation
1624 * Use native HTML5 History API Implementation
1628 * History.onPopState(event,extra)
1629 * Refresh the Current State
1631 History.onPopState = function(event,extra){
1633 var stateId = false, newState = false, currentHash, currentState;
1635 // Reset the double check
1636 History.doubleCheckComplete();
1638 // Check for a Hash, and handle apporiatly
1639 currentHash = History.getHash();
1640 if ( currentHash ) {
1642 currentState = History.extractState(currentHash||History.getLocationHref(),true);
1643 if ( currentState ) {
1644 // We were able to parse it, it must be a State!
1645 // Let's forward to replaceState
1646 //History.debug('History.onPopState: state anchor', currentHash, currentState);
1647 History.replaceState(currentState.data, currentState.title, currentState.url, false);
1650 // Traditional Anchor
1651 //History.debug('History.onPopState: traditional anchor', currentHash);
1652 History.Adapter.trigger(window,'anchorchange');
1653 History.busy(false);
1656 // We don't care for hashes
1657 History.expectedStateId = false;
1662 stateId = History.Adapter.extractEventData('state',event,extra) || false;
1666 // Vanilla: Back/forward button was used
1667 newState = History.getStateById(stateId);
1669 else if ( History.expectedStateId ) {
1670 // Vanilla: A new state was pushed, and popstate was called manually
1671 newState = History.getStateById(History.expectedStateId);
1675 newState = History.extractState(History.getLocationHref());
1678 // The State did not exist in our store
1680 // Regenerate the State
1681 newState = History.createStateObject(null,null,History.getLocationHref());
1685 History.expectedStateId = false;
1687 // Check if we are the same state
1688 if ( History.isLastSavedState(newState) ) {
1689 // There has been no change (just the page's hash has finally propagated)
1690 //History.debug('History.onPopState: no change', newState, History.savedStates);
1691 History.busy(false);
1696 History.storeState(newState);
1697 History.saveState(newState);
1699 // Force update of the title
1700 History.setTitle(newState);
1703 History.Adapter.trigger(window,'statechange');
1704 History.busy(false);
1709 History.Adapter.bind(window,'popstate',History.onPopState);
1712 * History.pushState(data,title,url)
1713 * Add a new State to the history object, become it, and trigger onpopstate
1714 * We have to trigger for HTML4 compatibility
1715 * @param {object} data
1716 * @param {string} title
1717 * @param {string} url
1720 History.pushState = function(data,title,url,queue){
1721 //History.debug('History.pushState: called', arguments);
1724 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1725 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1729 if ( queue !== false && History.busy() ) {
1730 // Wait + Push to Queue
1731 //History.debug('History.pushState: we must wait', arguments);
1734 callback: History.pushState,
1741 // Make Busy + Continue
1744 // Create the newState
1745 var newState = History.createStateObject(data,title,url);
1748 if ( History.isLastSavedState(newState) ) {
1749 // Won't be a change
1750 History.busy(false);
1753 // Store the newState
1754 History.storeState(newState);
1755 History.expectedStateId = newState.id;
1757 // Push the newState
1758 history.pushState(newState.id,newState.title,newState.url);
1761 History.Adapter.trigger(window,'popstate');
1764 // End pushState closure
1769 * History.replaceState(data,title,url)
1770 * Replace the State and trigger onpopstate
1771 * We have to trigger for HTML4 compatibility
1772 * @param {object} data
1773 * @param {string} title
1774 * @param {string} url
1777 History.replaceState = function(data,title,url,queue){
1778 //History.debug('History.replaceState: called', arguments);
1781 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1782 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1786 if ( queue !== false && History.busy() ) {
1787 // Wait + Push to Queue
1788 //History.debug('History.replaceState: we must wait', arguments);
1791 callback: History.replaceState,
1798 // Make Busy + Continue
1801 // Create the newState
1802 var newState = History.createStateObject(data,title,url);
1805 if ( History.isLastSavedState(newState) ) {
1806 // Won't be a change
1807 History.busy(false);
1810 // Store the newState
1811 History.storeState(newState);
1812 History.expectedStateId = newState.id;
1814 // Push the newState
1815 history.replaceState(newState.id,newState.title,newState.url);
1818 History.Adapter.trigger(window,'popstate');
1821 // End replaceState closure
1825 } // !History.emulated.pushState
1828 // ====================================================================
1834 if ( sessionStorage ) {
1837 History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
1844 History.normalizeStore();
1849 History.normalizeStore();
1853 * Clear Intervals on exit to prevent memory leaks
1855 History.Adapter.bind(window,"unload",History.clearAllIntervals);
1858 * Create the initial State
1860 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
1863 * Bind for Saving Store
1865 if ( sessionStorage ) {
1866 // When the page is closed
1867 History.onUnload = function(){
1869 var currentStore, item, currentStoreString;
1873 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1880 currentStore.idToState = currentStore.idToState || {};
1881 currentStore.urlToId = currentStore.urlToId || {};
1882 currentStore.stateToId = currentStore.stateToId || {};
1885 for ( item in History.idToState ) {
1886 if ( !History.idToState.hasOwnProperty(item) ) {
1889 currentStore.idToState[item] = History.idToState[item];
1891 for ( item in History.urlToId ) {
1892 if ( !History.urlToId.hasOwnProperty(item) ) {
1895 currentStore.urlToId[item] = History.urlToId[item];
1897 for ( item in History.stateToId ) {
1898 if ( !History.stateToId.hasOwnProperty(item) ) {
1901 currentStore.stateToId[item] = History.stateToId[item];
1905 History.store = currentStore;
1906 History.normalizeStore();
1908 // In Safari, going into Private Browsing mode causes the
1909 // Session Storage object to still exist but if you try and use
1910 // or set any property/function of it it throws the exception
1911 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1912 // add something to storage that exceeded the quota." infinitely
1914 currentStoreString = JSON.stringify(currentStore);
1917 sessionStorage.setItem('History.store', currentStoreString);
1920 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1921 if (sessionStorage.length) {
1922 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1923 // removing/resetting the storage can work.
1924 sessionStorage.removeItem('History.store');
1925 sessionStorage.setItem('History.store', currentStoreString);
1927 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1935 // For Internet Explorer
1936 History.intervalList.push(setInterval(History.onUnload,History.options.storeInterval));
1938 // For Other Browsers
1939 History.Adapter.bind(window,'beforeunload',History.onUnload);
1940 History.Adapter.bind(window,'unload',History.onUnload);
1942 // Both are enabled for consistency
1945 // Non-Native pushState Implementation
1946 if ( !History.emulated.pushState ) {
1947 // Be aware, the following is only for native pushState implementations
1948 // If you are wanting to include something for all browsers
1949 // Then include it above this if block
1954 if ( History.bugs.safariPoll ) {
1955 History.intervalList.push(setInterval(History.safariStatePoll, History.options.safariPollInterval));
1959 * Ensure Cross Browser Compatibility
1961 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1963 * Fix Safari HashChange Issue
1967 History.Adapter.bind(window,'hashchange',function(){
1968 History.Adapter.trigger(window,'popstate');
1972 if ( History.getHash() ) {
1973 History.Adapter.onDomLoad(function(){
1974 History.Adapter.trigger(window,'hashchange');
1979 } // !History.emulated.pushState
1982 }; // History.initCore
1984 // Try to Initialise History
1985 if (!History.options || !History.options.delayInit) {