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,
162 Roo.apply(this,options)
164 // Check Load Status of Adapter
165 //if ( typeof this.Adapter === 'undefined' ) {
169 // Check Load Status of Core
170 if ( typeof this.initCore !== 'undefined' ) {
174 // Check Load Status of HTML4 Support
175 if ( typeof this.initHtml4 !== 'undefined' ) {
182 * Is History enabled?
184 this.enabled = !this.emulated.pushState;
188 // ====================================================================
193 * The store for all session specific data
199 * 1-1: State ID to State Object
201 History.idToState = History.idToState||{};
205 * 1-1: State String to State ID
207 History.stateToId = History.stateToId||{};
211 * 1-1: State URL to State ID
213 History.urlToId = History.urlToId||{};
216 * History.storedStates
217 * Store the states in an array
219 History.storedStates = History.storedStates||[];
222 * History.savedStates
223 * Saved the states in an array
225 History.savedStates = History.savedStates||[];
239 // ========================================================================
243 initCore : function(options){
245 this.intervalList = [];
249 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
250 this.sessionStorage.setItem('TEST', '1');
251 this.sessionStorage.removeItem('TEST');
253 this.sessionStorage = false;
257 if ( typeof this.initCore.initialized !== 'undefined' ) {
262 this.initCore.initialized = true;
270 * History.clearAllIntervals
271 * Clears all setInterval instances.
273 clearAllIntervals: function()
275 var i, il = this.intervalList;
276 if (typeof il !== "undefined" && il !== null) {
277 for (i = 0; i < il.length; i++) {
278 clearInterval(il[i]);
280 this.intervalList = null;
285 // ====================================================================
289 * History.debugLog(message,...)
290 * Logs the passed arguments if debug enabled
292 debugLog : function()
294 if ( (this.debug||false) ) {
295 Roo.log.apply(History,arguments);
301 // ====================================================================
305 * History.getInternetExplorerMajorVersion()
306 * Get's the major version of Internet Explorer
308 * @license Public Domain
309 * @author Benjamin Arthur Lupton <contact@balupton.com>
310 * @author James Padolsey <https://gist.github.com/527683>
312 getInternetExplorerMajorVersion : function(){
313 var result = this.getInternetExplorerMajorVersion.cached =
314 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
315 ? this.getInternetExplorerMajorVersion.cached
318 div = window.document.createElement('div'),
319 all = div.getElementsByTagName('i');
320 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
321 return (v > 4) ? v : false;
328 * isInternetExplorer()
329 * Are we using Internet Explorer?
331 * @license Public Domain
332 * @author Benjamin Arthur Lupton <contact@balupton.com>
334 isInternetExplorer : function(){
336 this.isInternetExplorer.cached =
337 (typeof this.isInternetExplorer.cached !== 'undefined')
338 ? this.isInternetExplorer.cached
339 : Boolean(this.getInternetExplorerMajorVersion())
345 * Which features require emulating?
353 initEmulated : function()
357 if (this.html4Mode) {
363 this.emulated.pushState = !Boolean(
364 window.history && window.history.pushState && window.history.replaceState
366 (/ 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) */
367 || (/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 */
370 this.emulated.hashChange = Boolean(
371 !(('onhashchange' in window) || ('onhashchange' in window.document))
373 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
382 * Checks to see if the Object is Empty
383 * @param {Object} obj
386 isEmptyObject = function(obj) {
387 for ( var name in obj ) {
388 if ( obj.hasOwnProperty(name) ) {
397 * Clones a object and eliminate all references to the original contexts
398 * @param {Object} obj
401 cloneObject = function(obj) {
404 hash = JSON.stringify(obj);
405 newObj = JSON.parse(hash);
414 // ====================================================================
419 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
420 * @return {String} rootUrl
422 getRootUrl = function(){
424 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
425 if ( window.document.location.port||false ) {
426 rootUrl += ':'+window.document.location.port;
436 * Fetches the `href` attribute of the `<base href="...">` element if it exists
437 * @return {String} baseHref
439 getBaseHref = function(){
442 baseElements = window.document.getElementsByTagName('base'),
446 // Test for Base Element
447 if ( baseElements.length === 1 ) {
448 // Prepare for Base Element
449 baseElement = baseElements[0];
450 baseHref = baseElement.href.replace(/[^\/]+$/,'');
453 // Adjust trailing slash
454 baseHref = baseHref.replace(/\/+$/,'');
455 if ( baseHref ) baseHref += '/';
463 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
464 * @return {String} baseUrl
466 getBaseUrl = function(){
468 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
476 * Fetches the URL of the current page
477 * @return {String} pageUrl
479 getPageUrl = function(){
482 State = this.getState(false,false),
483 stateUrl = (State||{}).url||this.getLocationHref(),
487 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
488 return (/\./).test(part) ? part : part+'/';
497 * Fetches the Url of the directory of the current page
498 * @return {String} basePageUrl
500 getBasePageUrl = function(){
502 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
503 return (/[^\/]$/).test(part) ? '' : part;
504 }).replace(/\/+$/,'')+'/';
512 * Ensures that we have an absolute URL and not a relative URL
513 * @param {string} url
514 * @param {Boolean} allowBaseHref
515 * @return {string} fullUrl
517 getFullUrl = function(url,allowBaseHref){
519 var fullUrl = url, firstChar = url.substring(0,1);
520 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
523 if ( /[a-z]+\:\/\//.test(url) ) {
526 else if ( firstChar === '/' ) {
528 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
530 else if ( firstChar === '#' ) {
532 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
534 else if ( firstChar === '?' ) {
536 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
540 if ( allowBaseHref ) {
541 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
543 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
545 // We have an if condition above as we do not want hashes
546 // which are relative to the baseHref in our URLs
547 // as if the baseHref changes, then all our bookmarks
548 // would now point to different locations
549 // whereas the basePageUrl will always stay the same
553 return fullUrl.replace(/\#$/,'');
558 * Ensures that we have a relative URL and not a absolute URL
559 * @param {string} url
560 * @return {string} url
562 getShortUrl = function(url){
564 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
567 if ( this.emulated.pushState ) {
568 // We are in a if statement as when pushState is not emulated
569 // The actual url these short urls are relative to can change
570 // So within the same session, we the url may end up somewhere different
571 shortUrl = shortUrl.replace(baseUrl,'');
575 shortUrl = shortUrl.replace(rootUrl,'/');
577 // Ensure we can still detect it as a state
578 if ( this.isTraditionalAnchor(shortUrl) ) {
579 shortUrl = './'+shortUrl;
583 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
590 * getLocationHref(document)
591 * Returns a normalized version of document.location.href
592 * accounting for browser inconsistencies, etc.
594 * This URL will be URI-encoded and will include the hash
596 * @param {object} document
597 * @return {string} url
599 getLocationHref = function(doc) {
600 doc = doc || window.document;
602 // most of the time, this will be true
603 if (doc.URL === doc.location.href)
604 return doc.location.href;
606 // some versions of webkit URI-decode document.location.href
607 // but they leave document.URL in an encoded state
608 if (doc.location.href === decodeURIComponent(doc.URL))
611 // FF 3.6 only updates document.URL when a page is reloaded
612 // document.location.href is updated correctly
613 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
614 return doc.location.href;
616 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
617 return doc.location.href;
619 return doc.URL || doc.location.href;
625 * History.noramlizeStore()
626 * Noramlize the store by adding necessary values
628 History.normalizeStore = function(){
629 History.store.idToState = History.store.idToState||{};
630 History.store.urlToId = History.store.urlToId||{};
631 History.store.stateToId = History.store.stateToId||{};
636 * Get an object containing the data, title and url of the current state
637 * @param {Boolean} friendly
638 * @param {Boolean} create
639 * @return {Object} State
641 History.getState = function(friendly,create){
643 if ( typeof friendly === 'undefined' ) { friendly = true; }
644 if ( typeof create === 'undefined' ) { create = true; }
647 var State = History.getLastSavedState();
650 if ( !State && create ) {
651 State = History.createStateObject();
656 State = History.cloneObject(State);
657 State.url = State.cleanUrl||State.url;
665 * History.getIdByState(State)
666 * Gets a ID for a State
667 * @param {State} newState
668 * @return {String} id
670 History.getIdByState = function(newState){
673 var id = History.extractId(newState.url),
677 // Find ID via State String
678 str = History.getStateString(newState);
679 if ( typeof History.stateToId[str] !== 'undefined' ) {
680 id = History.stateToId[str];
682 else if ( typeof History.store.stateToId[str] !== 'undefined' ) {
683 id = History.store.stateToId[str];
688 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
689 if ( typeof History.idToState[id] === 'undefined' && typeof History.store.idToState[id] === 'undefined' ) {
694 // Apply the new State to the ID
695 History.stateToId[str] = id;
696 History.idToState[id] = newState;
705 * History.normalizeState(State)
706 * Expands a State Object
707 * @param {object} State
710 History.normalizeState = function(oldState){
712 var newState, dataNotEmpty;
715 if ( !oldState || (typeof oldState !== 'object') ) {
720 if ( typeof oldState.normalized !== 'undefined' ) {
725 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
729 // ----------------------------------------------------------------
733 newState.normalized = true;
734 newState.title = oldState.title||'';
735 newState.url = History.getFullUrl(oldState.url?oldState.url:(History.getLocationHref()));
736 newState.hash = History.getShortUrl(newState.url);
737 newState.data = History.cloneObject(oldState.data);
740 newState.id = History.getIdByState(newState);
742 // ----------------------------------------------------------------
745 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
746 newState.url = newState.cleanUrl;
748 // Check to see if we have more than just a url
749 dataNotEmpty = !History.isEmptyObject(newState.data);
752 if ( (newState.title || dataNotEmpty) && History.options.disableSuid !== true ) {
754 newState.hash = History.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
755 if ( !/\?/.test(newState.hash) ) {
756 newState.hash += '?';
758 newState.hash += '&_suid='+newState.id;
761 // Create the Hashed URL
762 newState.hashedUrl = History.getFullUrl(newState.hash);
764 // ----------------------------------------------------------------
766 // Update the URL if we have a duplicate
767 if ( (History.emulated.pushState || History.bugs.safariPoll) && History.hasUrlDuplicate(newState) ) {
768 newState.url = newState.hashedUrl;
771 // ----------------------------------------------------------------
778 * History.createStateObject(data,title,url)
779 * Creates a object based on the data, title and url state params
780 * @param {object} data
781 * @param {string} title
782 * @param {string} url
785 History.createStateObject = function(data,title,url){
794 State = History.normalizeState(State);
801 * History.getStateById(id)
802 * Get a state by it's UID
805 History.getStateById = function(id){
810 var State = History.idToState[id] || History.store.idToState[id] || undefined;
817 * Get a State's String
818 * @param {State} passedState
820 History.getStateString = function(passedState){
822 var State, cleanedState, str;
825 State = History.normalizeState(passedState);
830 title: passedState.title,
835 str = JSON.stringify(cleanedState);
843 * @param {State} passedState
844 * @return {String} id
846 History.getStateId = function(passedState){
851 State = History.normalizeState(passedState);
861 * History.getHashByState(State)
862 * Creates a Hash for the State Object
863 * @param {State} passedState
864 * @return {String} hash
866 History.getHashByState = function(passedState){
871 State = History.normalizeState(passedState);
881 * History.extractId(url_or_hash)
882 * Get a State ID by it's URL or Hash
883 * @param {string} url_or_hash
884 * @return {string} id
886 History.extractId = function ( url_or_hash ) {
888 var id,parts,url, tmp;
892 // If the URL has a #, use the id from before the #
893 if (url_or_hash.indexOf('#') != -1)
895 tmp = url_or_hash.split("#")[0];
902 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
903 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
904 id = parts ? String(parts[2]||'') : '';
911 * History.isTraditionalAnchor
912 * Checks to see if the url is a traditional anchor or not
913 * @param {String} url_or_hash
916 History.isTraditionalAnchor = function(url_or_hash){
918 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
921 return isTraditional;
925 * History.extractState
926 * Get a State by it's URL or Hash
927 * @param {String} url_or_hash
928 * @return {State|null}
930 History.extractState = function(url_or_hash,create){
932 var State = null, id, url;
933 create = create||false;
936 id = History.extractId(url_or_hash);
938 State = History.getStateById(id);
941 // Fetch SUID returned no State
944 url = History.getFullUrl(url_or_hash);
947 id = History.getIdByUrl(url)||false;
949 State = History.getStateById(id);
953 if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) {
954 State = History.createStateObject(null,null,url);
963 * History.getIdByUrl()
964 * Get a State ID by a State URL
966 History.getIdByUrl = function(url){
968 var id = History.urlToId[url] || History.store.urlToId[url] || undefined;
975 * History.getLastSavedState()
976 * Get an object containing the data, title and url of the current state
977 * @return {Object} State
979 History.getLastSavedState = function(){
980 return History.savedStates[History.savedStates.length-1]||undefined;
984 * History.getLastStoredState()
985 * Get an object containing the data, title and url of the current state
986 * @return {Object} State
988 History.getLastStoredState = function(){
989 return History.storedStates[History.storedStates.length-1]||undefined;
993 * History.hasUrlDuplicate
994 * Checks if a Url will have a url conflict
995 * @param {Object} newState
996 * @return {Boolean} hasDuplicate
998 History.hasUrlDuplicate = function(newState) {
1000 var hasDuplicate = false,
1004 oldState = History.extractState(newState.url);
1007 hasDuplicate = oldState && oldState.id !== newState.id;
1010 return hasDuplicate;
1014 * History.storeState
1016 * @param {Object} newState
1017 * @return {Object} newState
1019 History.storeState = function(newState){
1021 History.urlToId[newState.url] = newState.id;
1024 History.storedStates.push(History.cloneObject(newState));
1031 * History.isLastSavedState(newState)
1032 * Tests to see if the state is the last state
1033 * @param {Object} newState
1034 * @return {boolean} isLast
1036 History.isLastSavedState = function(newState){
1039 newId, oldState, oldId;
1042 if ( History.savedStates.length ) {
1043 newId = newState.id;
1044 oldState = History.getLastSavedState();
1045 oldId = oldState.id;
1048 isLast = (newId === oldId);
1058 * @param {Object} newState
1059 * @return {boolean} changed
1061 History.saveState = function(newState){
1063 if ( History.isLastSavedState(newState) ) {
1068 History.savedStates.push(History.cloneObject(newState));
1075 * History.getStateByIndex()
1076 * Gets a state by the index
1077 * @param {integer} index
1080 History.getStateByIndex = function(index){
1085 if ( typeof index === 'undefined' ) {
1086 // Get the last inserted
1087 State = History.savedStates[History.savedStates.length-1];
1089 else if ( index < 0 ) {
1091 State = History.savedStates[History.savedStates.length+index];
1094 // Get from the beginning
1095 State = History.savedStates[index];
1103 * History.getCurrentIndex()
1104 * Gets the current index
1107 History.getCurrentIndex = function(){
1112 if(History.savedStates.length < 1) {
1116 index = History.savedStates.length-1;
1121 // ====================================================================
1126 * @param {Location=} location
1127 * Gets the current document hash
1128 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1131 History.getHash = function(doc){
1132 var url = History.getLocationHref(doc),
1134 hash = History.getHashByUrl(url);
1139 * History.unescapeHash()
1140 * normalize and Unescape a Hash
1141 * @param {String} hash
1144 History.unescapeHash = function(hash){
1146 var result = History.normalizeHash(hash);
1149 result = decodeURIComponent(result);
1156 * History.normalizeHash()
1157 * normalize a hash across browsers
1160 History.normalizeHash = function(hash){
1162 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1169 * History.setHash(hash)
1170 * Sets the document hash
1171 * @param {string} hash
1174 History.setHash = function(hash,queue){
1179 if ( queue !== false && History.busy() ) {
1180 // Wait + Push to Queue
1181 //History.debug('History.setHash: we must wait', arguments);
1184 callback: History.setHash,
1192 //History.debug('History.setHash: called',hash);
1194 // Make Busy + Continue
1197 // Check if hash is a state
1198 State = History.extractState(hash,true);
1199 if ( State && !History.emulated.pushState ) {
1200 // Hash is a state so skip the setHash
1201 //History.debug('History.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1204 History.pushState(State.data,State.title,State.url,false);
1206 else if ( History.getHash() !== hash ) {
1207 // Hash is a proper hash, so apply it
1209 // Handle browser bugs
1210 if ( History.bugs.setHash ) {
1211 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1213 // Fetch the base page
1214 pageUrl = History.getPageUrl();
1216 // Safari hash apply
1217 History.pushState(null,null,pageUrl+'#'+hash,false);
1220 // Normal hash apply
1221 window.document.location.hash = hash;
1231 * normalize and Escape a Hash
1234 History.escapeHash = function(hash){
1236 var result = History.normalizeHash(hash);
1239 result = window.encodeURIComponent(result);
1242 if ( !History.bugs.hashEscape ) {
1243 // Restore common parts
1245 .replace(/\%21/g,'!')
1246 .replace(/\%26/g,'&')
1247 .replace(/\%3D/g,'=')
1248 .replace(/\%3F/g,'?');
1256 * History.getHashByUrl(url)
1257 * Extracts the Hash from a URL
1258 * @param {string} url
1259 * @return {string} url
1261 History.getHashByUrl = function(url){
1263 var hash = String(url)
1264 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1268 hash = History.unescapeHash(hash);
1275 * History.setTitle(title)
1276 * Applies the title to the document
1277 * @param {State} newState
1280 History.setTitle = function(newState){
1282 var title = newState.title,
1287 firstState = History.getStateByIndex(0);
1288 if ( firstState && firstState.url === newState.url ) {
1289 title = firstState.title||History.options.initialTitle;
1295 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1297 catch ( Exception ) { }
1298 window.document.title = title;
1305 // ====================================================================
1310 * The list of queues to use
1311 * First In, First Out
1313 History.queues = [];
1316 * History.busy(value)
1317 * @param {boolean} value [optional]
1318 * @return {boolean} busy
1320 History.busy = function(value){
1322 if ( typeof value !== 'undefined' ) {
1323 //History.debug('History.busy: changing ['+(History.busy.flag||false)+'] to ['+(value||false)+']', History.queues.length);
1324 History.busy.flag = value;
1327 else if ( typeof History.busy.flag === 'undefined' ) {
1328 History.busy.flag = false;
1332 if ( !History.busy.flag ) {
1333 // Execute the next item in the queue
1334 window.clearTimeout(History.busy.timeout);
1335 var fireNext = function(){
1337 if ( History.busy.flag ) return;
1338 for ( i=History.queues.length-1; i >= 0; --i ) {
1339 queue = History.queues[i];
1340 if ( queue.length === 0 ) continue;
1341 item = queue.shift();
1342 History.fireQueueItem(item);
1343 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1346 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1350 return History.busy.flag;
1356 History.busy.flag = false;
1359 * History.fireQueueItem(item)
1361 * @param {Object} item
1362 * @return {Mixed} result
1364 History.fireQueueItem = function(item){
1365 return item.callback.apply(item.scope||History,item.args||[]);
1369 * History.pushQueue(callback,args)
1370 * Add an item to the queue
1371 * @param {Object} item [scope,callback,args,queue]
1373 History.pushQueue = function(item){
1374 // Prepare the queue
1375 History.queues[item.queue||0] = History.queues[item.queue||0]||[];
1378 History.queues[item.queue||0].push(item);
1385 * History.queue (item,queue), (func,queue), (func), (item)
1386 * Either firs the item now if not busy, or adds it to the queue
1388 History.queue = function(item,queue){
1390 if ( typeof item === 'function' ) {
1395 if ( typeof queue !== 'undefined' ) {
1400 if ( History.busy() ) {
1401 History.pushQueue(item);
1403 History.fireQueueItem(item);
1411 * History.clearQueue()
1414 History.clearQueue = function(){
1415 History.busy.flag = false;
1416 History.queues = [];
1421 // ====================================================================
1425 * History.stateChanged
1426 * States whether or not the state has changed since the last double check was initialised
1428 History.stateChanged = false;
1431 * History.doubleChecker
1432 * Contains the timeout used for the double checks
1434 History.doubleChecker = false;
1437 * History.doubleCheckComplete()
1438 * Complete a double check
1441 History.doubleCheckComplete = function(){
1443 History.stateChanged = true;
1446 History.doubleCheckClear();
1453 * History.doubleCheckClear()
1454 * Clear a double check
1457 History.doubleCheckClear = function(){
1459 if ( History.doubleChecker ) {
1460 window.clearTimeout(History.doubleChecker);
1461 History.doubleChecker = false;
1469 * History.doubleCheck()
1470 * Create a double check
1473 History.doubleCheck = function(tryAgain){
1475 History.stateChanged = false;
1476 History.doubleCheckClear();
1478 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1479 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1480 if ( History.bugs.ieDoubleCheck ) {
1482 History.doubleChecker = window.setTimeout(
1484 History.doubleCheckClear();
1485 if ( !History.stateChanged ) {
1486 //History.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1492 History.options.doubleCheckInterval
1501 // ====================================================================
1505 * History.safariStatePoll()
1506 * Poll the current state
1509 History.safariStatePoll = function(){
1512 // Get the Last State which has the new URL
1514 urlState = History.extractState(History.getLocationHref()),
1517 // Check for a difference
1518 if ( !History.isLastSavedState(urlState) ) {
1519 newState = urlState;
1525 // Check if we have a state with that url
1528 //History.debug('History.safariStatePoll: new');
1529 newState = History.createStateObject();
1532 // Apply the New State
1533 //History.debug('History.safariStatePoll: trigger');
1534 History.Adapter.trigger(window,'popstate');
1541 // ====================================================================
1545 * History.back(queue)
1546 * Send the browser history back one item
1547 * @param {Integer} queue [optional]
1549 History.back = function(queue){
1550 //History.debug('History.back: called', arguments);
1553 if ( queue !== false && History.busy() ) {
1554 // Wait + Push to Queue
1555 //History.debug('History.back: we must wait', arguments);
1558 callback: History.back,
1565 // Make Busy + Continue
1568 // Fix certain browser bugs that prevent the state from changing
1569 History.doubleCheck(function(){
1570 History.back(false);
1581 * History.forward(queue)
1582 * Send the browser history forward one item
1583 * @param {Integer} queue [optional]
1585 History.forward = function(queue){
1586 //History.debug('History.forward: called', arguments);
1589 if ( queue !== false && History.busy() ) {
1590 // Wait + Push to Queue
1591 //History.debug('History.forward: we must wait', arguments);
1594 callback: History.forward,
1601 // Make Busy + Continue
1604 // Fix certain browser bugs that prevent the state from changing
1605 History.doubleCheck(function(){
1606 History.forward(false);
1612 // End forward closure
1617 * History.go(index,queue)
1618 * Send the browser history back or forward index times
1619 * @param {Integer} queue [optional]
1621 History.go = function(index,queue){
1622 //History.debug('History.go: called', arguments);
1630 for ( i=1; i<=index; ++i ) {
1631 History.forward(queue);
1634 else if ( index < 0 ) {
1636 for ( i=-1; i>=index; --i ) {
1637 History.back(queue);
1641 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1649 // ====================================================================
1650 // HTML5 State Support
1652 // Non-Native pushState Implementation
1653 if ( History.emulated.pushState ) {
1655 * Provide Skeleton for HTML4 Browsers
1659 var emptyFunction = function(){};
1660 History.pushState = History.pushState||emptyFunction;
1661 History.replaceState = History.replaceState||emptyFunction;
1662 } // History.emulated.pushState
1664 // Native pushState Implementation
1667 * Use native HTML5 History API Implementation
1671 * History.onPopState(event,extra)
1672 * Refresh the Current State
1674 History.onPopState = function(event,extra){
1676 var stateId = false, newState = false, currentHash, currentState;
1678 // Reset the double check
1679 History.doubleCheckComplete();
1681 // Check for a Hash, and handle apporiatly
1682 currentHash = History.getHash();
1683 if ( currentHash ) {
1685 currentState = History.extractState(currentHash||History.getLocationHref(),true);
1686 if ( currentState ) {
1687 // We were able to parse it, it must be a State!
1688 // Let's forward to replaceState
1689 //History.debug('History.onPopState: state anchor', currentHash, currentState);
1690 History.replaceState(currentState.data, currentState.title, currentState.url, false);
1693 // Traditional Anchor
1694 //History.debug('History.onPopState: traditional anchor', currentHash);
1695 History.Adapter.trigger(window,'anchorchange');
1696 History.busy(false);
1699 // We don't care for hashes
1700 History.expectedStateId = false;
1705 stateId = History.Adapter.extractEventData('state',event,extra) || false;
1709 // Vanilla: Back/forward button was used
1710 newState = History.getStateById(stateId);
1712 else if ( History.expectedStateId ) {
1713 // Vanilla: A new state was pushed, and popstate was called manually
1714 newState = History.getStateById(History.expectedStateId);
1718 newState = History.extractState(History.getLocationHref());
1721 // The State did not exist in our store
1723 // Regenerate the State
1724 newState = History.createStateObject(null,null,History.getLocationHref());
1728 History.expectedStateId = false;
1730 // Check if we are the same state
1731 if ( History.isLastSavedState(newState) ) {
1732 // There has been no change (just the page's hash has finally propagated)
1733 //History.debug('History.onPopState: no change', newState, History.savedStates);
1734 History.busy(false);
1739 History.storeState(newState);
1740 History.saveState(newState);
1742 // Force update of the title
1743 History.setTitle(newState);
1746 History.Adapter.trigger(window,'statechange');
1747 History.busy(false);
1752 History.Adapter.bind(window,'popstate',History.onPopState);
1755 * History.pushState(data,title,url)
1756 * Add a new State to the history object, become it, and trigger onpopstate
1757 * We have to trigger for HTML4 compatibility
1758 * @param {object} data
1759 * @param {string} title
1760 * @param {string} url
1763 History.pushState = function(data,title,url,queue){
1764 //History.debug('History.pushState: called', arguments);
1767 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1768 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1772 if ( queue !== false && History.busy() ) {
1773 // Wait + Push to Queue
1774 //History.debug('History.pushState: we must wait', arguments);
1777 callback: History.pushState,
1784 // Make Busy + Continue
1787 // Create the newState
1788 var newState = History.createStateObject(data,title,url);
1791 if ( History.isLastSavedState(newState) ) {
1792 // Won't be a change
1793 History.busy(false);
1796 // Store the newState
1797 History.storeState(newState);
1798 History.expectedStateId = newState.id;
1800 // Push the newState
1801 history.pushState(newState.id,newState.title,newState.url);
1804 History.Adapter.trigger(window,'popstate');
1807 // End pushState closure
1812 * History.replaceState(data,title,url)
1813 * Replace the State and trigger onpopstate
1814 * We have to trigger for HTML4 compatibility
1815 * @param {object} data
1816 * @param {string} title
1817 * @param {string} url
1820 History.replaceState = function(data,title,url,queue){
1821 //History.debug('History.replaceState: called', arguments);
1824 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1825 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1829 if ( queue !== false && History.busy() ) {
1830 // Wait + Push to Queue
1831 //History.debug('History.replaceState: we must wait', arguments);
1834 callback: History.replaceState,
1841 // Make Busy + Continue
1844 // Create the newState
1845 var newState = History.createStateObject(data,title,url);
1848 if ( History.isLastSavedState(newState) ) {
1849 // Won't be a change
1850 History.busy(false);
1853 // Store the newState
1854 History.storeState(newState);
1855 History.expectedStateId = newState.id;
1857 // Push the newState
1858 history.replaceState(newState.id,newState.title,newState.url);
1861 History.Adapter.trigger(window,'popstate');
1864 // End replaceState closure
1868 } // !History.emulated.pushState
1871 // ====================================================================
1877 if ( sessionStorage ) {
1880 History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
1887 History.normalizeStore();
1892 History.normalizeStore();
1896 * Clear Intervals on exit to prevent memory leaks
1898 History.Adapter.bind(window,"unload",History.clearAllIntervals);
1901 * Create the initial State
1903 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
1906 * Bind for Saving Store
1908 if ( sessionStorage ) {
1909 // When the page is closed
1910 History.onUnload = function(){
1912 var currentStore, item, currentStoreString;
1916 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1923 currentStore.idToState = currentStore.idToState || {};
1924 currentStore.urlToId = currentStore.urlToId || {};
1925 currentStore.stateToId = currentStore.stateToId || {};
1928 for ( item in History.idToState ) {
1929 if ( !History.idToState.hasOwnProperty(item) ) {
1932 currentStore.idToState[item] = History.idToState[item];
1934 for ( item in History.urlToId ) {
1935 if ( !History.urlToId.hasOwnProperty(item) ) {
1938 currentStore.urlToId[item] = History.urlToId[item];
1940 for ( item in History.stateToId ) {
1941 if ( !History.stateToId.hasOwnProperty(item) ) {
1944 currentStore.stateToId[item] = History.stateToId[item];
1948 History.store = currentStore;
1949 History.normalizeStore();
1951 // In Safari, going into Private Browsing mode causes the
1952 // Session Storage object to still exist but if you try and use
1953 // or set any property/function of it it throws the exception
1954 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1955 // add something to storage that exceeded the quota." infinitely
1957 currentStoreString = JSON.stringify(currentStore);
1960 sessionStorage.setItem('History.store', currentStoreString);
1963 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1964 if (sessionStorage.length) {
1965 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1966 // removing/resetting the storage can work.
1967 sessionStorage.removeItem('History.store');
1968 sessionStorage.setItem('History.store', currentStoreString);
1970 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1978 // For Internet Explorer
1979 History.intervalList.push(setInterval(History.onUnload,History.options.storeInterval));
1981 // For Other Browsers
1982 History.Adapter.bind(window,'beforeunload',History.onUnload);
1983 History.Adapter.bind(window,'unload',History.onUnload);
1985 // Both are enabled for consistency
1988 // Non-Native pushState Implementation
1989 if ( !History.emulated.pushState ) {
1990 // Be aware, the following is only for native pushState implementations
1991 // If you are wanting to include something for all browsers
1992 // Then include it above this if block
1997 if ( History.bugs.safariPoll ) {
1998 History.intervalList.push(setInterval(History.safariStatePoll, History.options.safariPollInterval));
2002 * Ensure Cross Browser Compatibility
2004 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
2006 * Fix Safari HashChange Issue
2010 History.Adapter.bind(window,'hashchange',function(){
2011 History.Adapter.trigger(window,'popstate');
2015 if ( History.getHash() ) {
2016 History.Adapter.onDomLoad(function(){
2017 History.Adapter.trigger(window,'hashchange');
2022 } // !History.emulated.pushState
2025 }; // History.initCore
2027 // Try to Initialise History
2028 if (!History.options || !History.options.delayInit) {