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/>
10 (function(window,undefined){
13 // ========================================================================
18 console = window.console||undefined, // Prevent a JSLint complain
19 document = window.document, // Make sure we are using the correct document
20 navigator = window.navigator, // Make sure we are using the correct navigator
21 sessionStorage = false, // sessionStorage
22 setTimeout = window.setTimeout,
23 clearTimeout = window.clearTimeout,
24 setInterval = window.setInterval,
25 clearInterval = window.clearInterval,
28 History = window.History = window.History||{}, // Public History Object
29 history = window.history; // Old History Object
32 sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
33 sessionStorage.setItem('TEST', '1');
34 sessionStorage.removeItem('TEST');
36 sessionStorage = false;
39 // MooTools Compatibility
40 JSON.stringify = JSON.stringify||JSON.encode;
41 JSON.parse = JSON.parse||JSON.decode;
44 if ( typeof History.init !== 'undefined' ) {
45 throw new Error('History.js Core has already been loaded...');
49 History.init = function(options){
50 // Check Load Status of Adapter
51 if ( typeof History.Adapter === 'undefined' ) {
55 // Check Load Status of Core
56 if ( typeof History.initCore !== 'undefined' ) {
60 // Check Load Status of HTML4 Support
61 if ( typeof History.initHtml4 !== 'undefined' ) {
70 // ========================================================================
74 History.initCore = function(options){
76 if ( typeof History.initCore.initialized !== 'undefined' ) {
81 History.initCore.initialized = true;
85 // ====================================================================
90 * Configurable options
92 History.options = History.options||{};
95 * History.options.hashChangeInterval
96 * How long should the interval be before hashchange checks
98 History.options.hashChangeInterval = History.options.hashChangeInterval || 100;
101 * History.options.safariPollInterval
102 * How long should the interval be before safari poll checks
104 History.options.safariPollInterval = History.options.safariPollInterval || 500;
107 * History.options.doubleCheckInterval
108 * How long should the interval be before we perform a double check
110 History.options.doubleCheckInterval = History.options.doubleCheckInterval || 500;
113 * History.options.disableSuid
114 * Force History not to append suid
116 History.options.disableSuid = History.options.disableSuid || false;
119 * History.options.storeInterval
120 * How long should we wait between store calls
122 History.options.storeInterval = History.options.storeInterval || 1000;
125 * History.options.busyDelay
126 * How long should we wait between busy events
128 History.options.busyDelay = History.options.busyDelay || 250;
131 * History.options.debug
132 * If true will enable debug messages to be logged
134 History.options.debug = History.options.debug || false;
137 * History.options.initialTitle
138 * What is the title of the initial state
140 History.options.initialTitle = History.options.initialTitle || document.title;
143 * History.options.html4Mode
144 * If true, will force HTMl4 mode (hashtags)
146 History.options.html4Mode = History.options.html4Mode || false;
149 * History.options.delayInit
150 * Want to override default options and call init manually.
152 History.options.delayInit = History.options.delayInit || false;
155 // ====================================================================
159 * History.intervalList
160 * List of intervals set, to be cleared when document is unloaded.
162 History.intervalList = [];
165 * History.clearAllIntervals
166 * Clears all setInterval instances.
168 History.clearAllIntervals = function(){
169 var i, il = History.intervalList;
170 if (typeof il !== "undefined" && il !== null) {
171 for (i = 0; i < il.length; i++) {
172 clearInterval(il[i]);
174 History.intervalList = null;
179 // ====================================================================
183 * History.debug(message,...)
184 * Logs the passed arguments if debug enabled
186 History.debug = function(){
187 if ( (History.options.debug||false) ) {
188 Roo.log.apply(History,arguments);
194 // ====================================================================
198 * History.getInternetExplorerMajorVersion()
199 * Get's the major version of Internet Explorer
201 * @license Public Domain
202 * @author Benjamin Arthur Lupton <contact@balupton.com>
203 * @author James Padolsey <https://gist.github.com/527683>
205 History.getInternetExplorerMajorVersion = function(){
206 var result = History.getInternetExplorerMajorVersion.cached =
207 (typeof History.getInternetExplorerMajorVersion.cached !== 'undefined')
208 ? History.getInternetExplorerMajorVersion.cached
211 div = document.createElement('div'),
212 all = div.getElementsByTagName('i');
213 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
214 return (v > 4) ? v : false;
221 * History.isInternetExplorer()
222 * Are we using Internet Explorer?
224 * @license Public Domain
225 * @author Benjamin Arthur Lupton <contact@balupton.com>
227 History.isInternetExplorer = function(){
229 History.isInternetExplorer.cached =
230 (typeof History.isInternetExplorer.cached !== 'undefined')
231 ? History.isInternetExplorer.cached
232 : Boolean(History.getInternetExplorerMajorVersion())
239 * Which features require emulating?
242 if (History.options.html4Mode) {
253 window.history && window.history.pushState && window.history.replaceState
255 (/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i).test(navigator.userAgent) /* disable for versions of iOS before version 4.3 (8F190) */
256 || (/AppleWebKit\/5([0-2]|3[0-2])/i).test(navigator.userAgent) /* disable for the mercury iOS browser, or at least older versions of the webkit engine */
260 !(('onhashchange' in window) || ('onhashchange' in document))
262 (History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8)
269 * Is History enabled?
271 History.enabled = !History.emulated.pushState;
275 * Which bugs are present
279 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
280 * https://bugs.webkit.org/show_bug.cgi?id=56249
282 setHash: Boolean(!History.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.' && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent)),
285 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
286 * https://bugs.webkit.org/show_bug.cgi?id=42940
288 safariPoll: Boolean(!History.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.' && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent)),
291 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
293 ieDoubleCheck: Boolean(History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8),
296 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
298 hashEscape: Boolean(History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 7)
302 * History.isEmptyObject(obj)
303 * Checks to see if the Object is Empty
304 * @param {Object} obj
307 History.isEmptyObject = function(obj) {
308 for ( var name in obj ) {
309 if ( obj.hasOwnProperty(name) ) {
317 * History.cloneObject(obj)
318 * Clones a object and eliminate all references to the original contexts
319 * @param {Object} obj
322 History.cloneObject = function(obj) {
325 hash = JSON.stringify(obj);
326 newObj = JSON.parse(hash);
335 // ====================================================================
339 * History.getRootUrl()
340 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
341 * @return {String} rootUrl
343 History.getRootUrl = function(){
345 var rootUrl = document.location.protocol+'//'+(document.location.hostname||document.location.host);
346 if ( document.location.port||false ) {
347 rootUrl += ':'+document.location.port;
356 * History.getBaseHref()
357 * Fetches the `href` attribute of the `<base href="...">` element if it exists
358 * @return {String} baseHref
360 History.getBaseHref = function(){
363 baseElements = document.getElementsByTagName('base'),
367 // Test for Base Element
368 if ( baseElements.length === 1 ) {
369 // Prepare for Base Element
370 baseElement = baseElements[0];
371 baseHref = baseElement.href.replace(/[^\/]+$/,'');
374 // Adjust trailing slash
375 baseHref = baseHref.replace(/\/+$/,'');
376 if ( baseHref ) baseHref += '/';
383 * History.getBaseUrl()
384 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
385 * @return {String} baseUrl
387 History.getBaseUrl = function(){
389 var baseUrl = History.getBaseHref()||History.getBasePageUrl()||History.getRootUrl();
396 * History.getPageUrl()
397 * Fetches the URL of the current page
398 * @return {String} pageUrl
400 History.getPageUrl = function(){
403 State = History.getState(false,false),
404 stateUrl = (State||{}).url||History.getLocationHref(),
408 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
409 return (/\./).test(part) ? part : part+'/';
417 * History.getBasePageUrl()
418 * Fetches the Url of the directory of the current page
419 * @return {String} basePageUrl
421 History.getBasePageUrl = function(){
423 var basePageUrl = (History.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
424 return (/[^\/]$/).test(part) ? '' : part;
425 }).replace(/\/+$/,'')+'/';
432 * History.getFullUrl(url)
433 * Ensures that we have an absolute URL and not a relative URL
434 * @param {string} url
435 * @param {Boolean} allowBaseHref
436 * @return {string} fullUrl
438 History.getFullUrl = function(url,allowBaseHref){
440 var fullUrl = url, firstChar = url.substring(0,1);
441 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
444 if ( /[a-z]+\:\/\//.test(url) ) {
447 else if ( firstChar === '/' ) {
449 fullUrl = History.getRootUrl()+url.replace(/^\/+/,'');
451 else if ( firstChar === '#' ) {
453 fullUrl = History.getPageUrl().replace(/#.*/,'')+url;
455 else if ( firstChar === '?' ) {
457 fullUrl = History.getPageUrl().replace(/[\?#].*/,'')+url;
461 if ( allowBaseHref ) {
462 fullUrl = History.getBaseUrl()+url.replace(/^(\.\/)+/,'');
464 fullUrl = History.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
466 // We have an if condition above as we do not want hashes
467 // which are relative to the baseHref in our URLs
468 // as if the baseHref changes, then all our bookmarks
469 // would now point to different locations
470 // whereas the basePageUrl will always stay the same
474 return fullUrl.replace(/\#$/,'');
478 * History.getShortUrl(url)
479 * Ensures that we have a relative URL and not a absolute URL
480 * @param {string} url
481 * @return {string} url
483 History.getShortUrl = function(url){
485 var shortUrl = url, baseUrl = History.getBaseUrl(), rootUrl = History.getRootUrl();
488 if ( History.emulated.pushState ) {
489 // We are in a if statement as when pushState is not emulated
490 // The actual url these short urls are relative to can change
491 // So within the same session, we the url may end up somewhere different
492 shortUrl = shortUrl.replace(baseUrl,'');
496 shortUrl = shortUrl.replace(rootUrl,'/');
498 // Ensure we can still detect it as a state
499 if ( History.isTraditionalAnchor(shortUrl) ) {
500 shortUrl = './'+shortUrl;
504 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
511 * History.getLocationHref(document)
512 * Returns a normalized version of document.location.href
513 * accounting for browser inconsistencies, etc.
515 * This URL will be URI-encoded and will include the hash
517 * @param {object} document
518 * @return {string} url
520 History.getLocationHref = function(doc) {
521 doc = doc || document;
523 // most of the time, this will be true
524 if (doc.URL === doc.location.href)
525 return doc.location.href;
527 // some versions of webkit URI-decode document.location.href
528 // but they leave document.URL in an encoded state
529 if (doc.location.href === decodeURIComponent(doc.URL))
532 // FF 3.6 only updates document.URL when a page is reloaded
533 // document.location.href is updated correctly
534 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
535 return doc.location.href;
537 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
538 return doc.location.href;
540 return doc.URL || doc.location.href;
544 // ====================================================================
549 * The store for all session specific data
555 * 1-1: State ID to State Object
557 History.idToState = History.idToState||{};
561 * 1-1: State String to State ID
563 History.stateToId = History.stateToId||{};
567 * 1-1: State URL to State ID
569 History.urlToId = History.urlToId||{};
572 * History.storedStates
573 * Store the states in an array
575 History.storedStates = History.storedStates||[];
578 * History.savedStates
579 * Saved the states in an array
581 History.savedStates = History.savedStates||[];
584 * History.noramlizeStore()
585 * Noramlize the store by adding necessary values
587 History.normalizeStore = function(){
588 History.store.idToState = History.store.idToState||{};
589 History.store.urlToId = History.store.urlToId||{};
590 History.store.stateToId = History.store.stateToId||{};
595 * Get an object containing the data, title and url of the current state
596 * @param {Boolean} friendly
597 * @param {Boolean} create
598 * @return {Object} State
600 History.getState = function(friendly,create){
602 if ( typeof friendly === 'undefined' ) { friendly = true; }
603 if ( typeof create === 'undefined' ) { create = true; }
606 var State = History.getLastSavedState();
609 if ( !State && create ) {
610 State = History.createStateObject();
615 State = History.cloneObject(State);
616 State.url = State.cleanUrl||State.url;
624 * History.getIdByState(State)
625 * Gets a ID for a State
626 * @param {State} newState
627 * @return {String} id
629 History.getIdByState = function(newState){
632 var id = History.extractId(newState.url),
636 // Find ID via State String
637 str = History.getStateString(newState);
638 if ( typeof History.stateToId[str] !== 'undefined' ) {
639 id = History.stateToId[str];
641 else if ( typeof History.store.stateToId[str] !== 'undefined' ) {
642 id = History.store.stateToId[str];
647 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
648 if ( typeof History.idToState[id] === 'undefined' && typeof History.store.idToState[id] === 'undefined' ) {
653 // Apply the new State to the ID
654 History.stateToId[str] = id;
655 History.idToState[id] = newState;
664 * History.normalizeState(State)
665 * Expands a State Object
666 * @param {object} State
669 History.normalizeState = function(oldState){
671 var newState, dataNotEmpty;
674 if ( !oldState || (typeof oldState !== 'object') ) {
679 if ( typeof oldState.normalized !== 'undefined' ) {
684 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
688 // ----------------------------------------------------------------
692 newState.normalized = true;
693 newState.title = oldState.title||'';
694 newState.url = History.getFullUrl(oldState.url?oldState.url:(History.getLocationHref()));
695 newState.hash = History.getShortUrl(newState.url);
696 newState.data = History.cloneObject(oldState.data);
699 newState.id = History.getIdByState(newState);
701 // ----------------------------------------------------------------
704 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
705 newState.url = newState.cleanUrl;
707 // Check to see if we have more than just a url
708 dataNotEmpty = !History.isEmptyObject(newState.data);
711 if ( (newState.title || dataNotEmpty) && History.options.disableSuid !== true ) {
713 newState.hash = History.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
714 if ( !/\?/.test(newState.hash) ) {
715 newState.hash += '?';
717 newState.hash += '&_suid='+newState.id;
720 // Create the Hashed URL
721 newState.hashedUrl = History.getFullUrl(newState.hash);
723 // ----------------------------------------------------------------
725 // Update the URL if we have a duplicate
726 if ( (History.emulated.pushState || History.bugs.safariPoll) && History.hasUrlDuplicate(newState) ) {
727 newState.url = newState.hashedUrl;
730 // ----------------------------------------------------------------
737 * History.createStateObject(data,title,url)
738 * Creates a object based on the data, title and url state params
739 * @param {object} data
740 * @param {string} title
741 * @param {string} url
744 History.createStateObject = function(data,title,url){
753 State = History.normalizeState(State);
760 * History.getStateById(id)
761 * Get a state by it's UID
764 History.getStateById = function(id){
769 var State = History.idToState[id] || History.store.idToState[id] || undefined;
776 * Get a State's String
777 * @param {State} passedState
779 History.getStateString = function(passedState){
781 var State, cleanedState, str;
784 State = History.normalizeState(passedState);
789 title: passedState.title,
794 str = JSON.stringify(cleanedState);
802 * @param {State} passedState
803 * @return {String} id
805 History.getStateId = function(passedState){
810 State = History.normalizeState(passedState);
820 * History.getHashByState(State)
821 * Creates a Hash for the State Object
822 * @param {State} passedState
823 * @return {String} hash
825 History.getHashByState = function(passedState){
830 State = History.normalizeState(passedState);
840 * History.extractId(url_or_hash)
841 * Get a State ID by it's URL or Hash
842 * @param {string} url_or_hash
843 * @return {string} id
845 History.extractId = function ( url_or_hash ) {
847 var id,parts,url, tmp;
851 // If the URL has a #, use the id from before the #
852 if (url_or_hash.indexOf('#') != -1)
854 tmp = url_or_hash.split("#")[0];
861 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
862 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
863 id = parts ? String(parts[2]||'') : '';
870 * History.isTraditionalAnchor
871 * Checks to see if the url is a traditional anchor or not
872 * @param {String} url_or_hash
875 History.isTraditionalAnchor = function(url_or_hash){
877 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
880 return isTraditional;
884 * History.extractState
885 * Get a State by it's URL or Hash
886 * @param {String} url_or_hash
887 * @return {State|null}
889 History.extractState = function(url_or_hash,create){
891 var State = null, id, url;
892 create = create||false;
895 id = History.extractId(url_or_hash);
897 State = History.getStateById(id);
900 // Fetch SUID returned no State
903 url = History.getFullUrl(url_or_hash);
906 id = History.getIdByUrl(url)||false;
908 State = History.getStateById(id);
912 if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) {
913 State = History.createStateObject(null,null,url);
922 * History.getIdByUrl()
923 * Get a State ID by a State URL
925 History.getIdByUrl = function(url){
927 var id = History.urlToId[url] || History.store.urlToId[url] || undefined;
934 * History.getLastSavedState()
935 * Get an object containing the data, title and url of the current state
936 * @return {Object} State
938 History.getLastSavedState = function(){
939 return History.savedStates[History.savedStates.length-1]||undefined;
943 * History.getLastStoredState()
944 * Get an object containing the data, title and url of the current state
945 * @return {Object} State
947 History.getLastStoredState = function(){
948 return History.storedStates[History.storedStates.length-1]||undefined;
952 * History.hasUrlDuplicate
953 * Checks if a Url will have a url conflict
954 * @param {Object} newState
955 * @return {Boolean} hasDuplicate
957 History.hasUrlDuplicate = function(newState) {
959 var hasDuplicate = false,
963 oldState = History.extractState(newState.url);
966 hasDuplicate = oldState && oldState.id !== newState.id;
975 * @param {Object} newState
976 * @return {Object} newState
978 History.storeState = function(newState){
980 History.urlToId[newState.url] = newState.id;
983 History.storedStates.push(History.cloneObject(newState));
990 * History.isLastSavedState(newState)
991 * Tests to see if the state is the last state
992 * @param {Object} newState
993 * @return {boolean} isLast
995 History.isLastSavedState = function(newState){
998 newId, oldState, oldId;
1001 if ( History.savedStates.length ) {
1002 newId = newState.id;
1003 oldState = History.getLastSavedState();
1004 oldId = oldState.id;
1007 isLast = (newId === oldId);
1017 * @param {Object} newState
1018 * @return {boolean} changed
1020 History.saveState = function(newState){
1022 if ( History.isLastSavedState(newState) ) {
1027 History.savedStates.push(History.cloneObject(newState));
1034 * History.getStateByIndex()
1035 * Gets a state by the index
1036 * @param {integer} index
1039 History.getStateByIndex = function(index){
1044 if ( typeof index === 'undefined' ) {
1045 // Get the last inserted
1046 State = History.savedStates[History.savedStates.length-1];
1048 else if ( index < 0 ) {
1050 State = History.savedStates[History.savedStates.length+index];
1053 // Get from the beginning
1054 State = History.savedStates[index];
1062 * History.getCurrentIndex()
1063 * Gets the current index
1066 History.getCurrentIndex = function(){
1071 if(History.savedStates.length < 1) {
1075 index = History.savedStates.length-1;
1080 // ====================================================================
1085 * @param {Location=} location
1086 * Gets the current document hash
1087 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1090 History.getHash = function(doc){
1091 var url = History.getLocationHref(doc),
1093 hash = History.getHashByUrl(url);
1098 * History.unescapeHash()
1099 * normalize and Unescape a Hash
1100 * @param {String} hash
1103 History.unescapeHash = function(hash){
1105 var result = History.normalizeHash(hash);
1108 result = decodeURIComponent(result);
1115 * History.normalizeHash()
1116 * normalize a hash across browsers
1119 History.normalizeHash = function(hash){
1121 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1128 * History.setHash(hash)
1129 * Sets the document hash
1130 * @param {string} hash
1133 History.setHash = function(hash,queue){
1138 if ( queue !== false && History.busy() ) {
1139 // Wait + Push to Queue
1140 //History.debug('History.setHash: we must wait', arguments);
1143 callback: History.setHash,
1151 //History.debug('History.setHash: called',hash);
1153 // Make Busy + Continue
1156 // Check if hash is a state
1157 State = History.extractState(hash,true);
1158 if ( State && !History.emulated.pushState ) {
1159 // Hash is a state so skip the setHash
1160 //History.debug('History.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1163 History.pushState(State.data,State.title,State.url,false);
1165 else if ( History.getHash() !== hash ) {
1166 // Hash is a proper hash, so apply it
1168 // Handle browser bugs
1169 if ( History.bugs.setHash ) {
1170 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1172 // Fetch the base page
1173 pageUrl = History.getPageUrl();
1175 // Safari hash apply
1176 History.pushState(null,null,pageUrl+'#'+hash,false);
1179 // Normal hash apply
1180 document.location.hash = hash;
1190 * normalize and Escape a Hash
1193 History.escapeHash = function(hash){
1195 var result = History.normalizeHash(hash);
1198 result = window.encodeURIComponent(result);
1201 if ( !History.bugs.hashEscape ) {
1202 // Restore common parts
1204 .replace(/\%21/g,'!')
1205 .replace(/\%26/g,'&')
1206 .replace(/\%3D/g,'=')
1207 .replace(/\%3F/g,'?');
1215 * History.getHashByUrl(url)
1216 * Extracts the Hash from a URL
1217 * @param {string} url
1218 * @return {string} url
1220 History.getHashByUrl = function(url){
1222 var hash = String(url)
1223 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1227 hash = History.unescapeHash(hash);
1234 * History.setTitle(title)
1235 * Applies the title to the document
1236 * @param {State} newState
1239 History.setTitle = function(newState){
1241 var title = newState.title,
1246 firstState = History.getStateByIndex(0);
1247 if ( firstState && firstState.url === newState.url ) {
1248 title = firstState.title||History.options.initialTitle;
1254 document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1256 catch ( Exception ) { }
1257 document.title = title;
1264 // ====================================================================
1269 * The list of queues to use
1270 * First In, First Out
1272 History.queues = [];
1275 * History.busy(value)
1276 * @param {boolean} value [optional]
1277 * @return {boolean} busy
1279 History.busy = function(value){
1281 if ( typeof value !== 'undefined' ) {
1282 //History.debug('History.busy: changing ['+(History.busy.flag||false)+'] to ['+(value||false)+']', History.queues.length);
1283 History.busy.flag = value;
1286 else if ( typeof History.busy.flag === 'undefined' ) {
1287 History.busy.flag = false;
1291 if ( !History.busy.flag ) {
1292 // Execute the next item in the queue
1293 clearTimeout(History.busy.timeout);
1294 var fireNext = function(){
1296 if ( History.busy.flag ) return;
1297 for ( i=History.queues.length-1; i >= 0; --i ) {
1298 queue = History.queues[i];
1299 if ( queue.length === 0 ) continue;
1300 item = queue.shift();
1301 History.fireQueueItem(item);
1302 History.busy.timeout = setTimeout(fireNext,History.options.busyDelay);
1305 History.busy.timeout = setTimeout(fireNext,History.options.busyDelay);
1309 return History.busy.flag;
1315 History.busy.flag = false;
1318 * History.fireQueueItem(item)
1320 * @param {Object} item
1321 * @return {Mixed} result
1323 History.fireQueueItem = function(item){
1324 return item.callback.apply(item.scope||History,item.args||[]);
1328 * History.pushQueue(callback,args)
1329 * Add an item to the queue
1330 * @param {Object} item [scope,callback,args,queue]
1332 History.pushQueue = function(item){
1333 // Prepare the queue
1334 History.queues[item.queue||0] = History.queues[item.queue||0]||[];
1337 History.queues[item.queue||0].push(item);
1344 * History.queue (item,queue), (func,queue), (func), (item)
1345 * Either firs the item now if not busy, or adds it to the queue
1347 History.queue = function(item,queue){
1349 if ( typeof item === 'function' ) {
1354 if ( typeof queue !== 'undefined' ) {
1359 if ( History.busy() ) {
1360 History.pushQueue(item);
1362 History.fireQueueItem(item);
1370 * History.clearQueue()
1373 History.clearQueue = function(){
1374 History.busy.flag = false;
1375 History.queues = [];
1380 // ====================================================================
1384 * History.stateChanged
1385 * States whether or not the state has changed since the last double check was initialised
1387 History.stateChanged = false;
1390 * History.doubleChecker
1391 * Contains the timeout used for the double checks
1393 History.doubleChecker = false;
1396 * History.doubleCheckComplete()
1397 * Complete a double check
1400 History.doubleCheckComplete = function(){
1402 History.stateChanged = true;
1405 History.doubleCheckClear();
1412 * History.doubleCheckClear()
1413 * Clear a double check
1416 History.doubleCheckClear = function(){
1418 if ( History.doubleChecker ) {
1419 clearTimeout(History.doubleChecker);
1420 History.doubleChecker = false;
1428 * History.doubleCheck()
1429 * Create a double check
1432 History.doubleCheck = function(tryAgain){
1434 History.stateChanged = false;
1435 History.doubleCheckClear();
1437 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1438 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1439 if ( History.bugs.ieDoubleCheck ) {
1441 History.doubleChecker = setTimeout(
1443 History.doubleCheckClear();
1444 if ( !History.stateChanged ) {
1445 //History.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1451 History.options.doubleCheckInterval
1460 // ====================================================================
1464 * History.safariStatePoll()
1465 * Poll the current state
1468 History.safariStatePoll = function(){
1471 // Get the Last State which has the new URL
1473 urlState = History.extractState(History.getLocationHref()),
1476 // Check for a difference
1477 if ( !History.isLastSavedState(urlState) ) {
1478 newState = urlState;
1484 // Check if we have a state with that url
1487 //History.debug('History.safariStatePoll: new');
1488 newState = History.createStateObject();
1491 // Apply the New State
1492 //History.debug('History.safariStatePoll: trigger');
1493 History.Adapter.trigger(window,'popstate');
1500 // ====================================================================
1504 * History.back(queue)
1505 * Send the browser history back one item
1506 * @param {Integer} queue [optional]
1508 History.back = function(queue){
1509 //History.debug('History.back: called', arguments);
1512 if ( queue !== false && History.busy() ) {
1513 // Wait + Push to Queue
1514 //History.debug('History.back: we must wait', arguments);
1517 callback: History.back,
1524 // Make Busy + Continue
1527 // Fix certain browser bugs that prevent the state from changing
1528 History.doubleCheck(function(){
1529 History.back(false);
1540 * History.forward(queue)
1541 * Send the browser history forward one item
1542 * @param {Integer} queue [optional]
1544 History.forward = function(queue){
1545 //History.debug('History.forward: called', arguments);
1548 if ( queue !== false && History.busy() ) {
1549 // Wait + Push to Queue
1550 //History.debug('History.forward: we must wait', arguments);
1553 callback: History.forward,
1560 // Make Busy + Continue
1563 // Fix certain browser bugs that prevent the state from changing
1564 History.doubleCheck(function(){
1565 History.forward(false);
1571 // End forward closure
1576 * History.go(index,queue)
1577 * Send the browser history back or forward index times
1578 * @param {Integer} queue [optional]
1580 History.go = function(index,queue){
1581 //History.debug('History.go: called', arguments);
1589 for ( i=1; i<=index; ++i ) {
1590 History.forward(queue);
1593 else if ( index < 0 ) {
1595 for ( i=-1; i>=index; --i ) {
1596 History.back(queue);
1600 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1608 // ====================================================================
1609 // HTML5 State Support
1611 // Non-Native pushState Implementation
1612 if ( History.emulated.pushState ) {
1614 * Provide Skeleton for HTML4 Browsers
1618 var emptyFunction = function(){};
1619 History.pushState = History.pushState||emptyFunction;
1620 History.replaceState = History.replaceState||emptyFunction;
1621 } // History.emulated.pushState
1623 // Native pushState Implementation
1626 * Use native HTML5 History API Implementation
1630 * History.onPopState(event,extra)
1631 * Refresh the Current State
1633 History.onPopState = function(event,extra){
1635 var stateId = false, newState = false, currentHash, currentState;
1637 // Reset the double check
1638 History.doubleCheckComplete();
1640 // Check for a Hash, and handle apporiatly
1641 currentHash = History.getHash();
1642 if ( currentHash ) {
1644 currentState = History.extractState(currentHash||History.getLocationHref(),true);
1645 if ( currentState ) {
1646 // We were able to parse it, it must be a State!
1647 // Let's forward to replaceState
1648 //History.debug('History.onPopState: state anchor', currentHash, currentState);
1649 History.replaceState(currentState.data, currentState.title, currentState.url, false);
1652 // Traditional Anchor
1653 //History.debug('History.onPopState: traditional anchor', currentHash);
1654 History.Adapter.trigger(window,'anchorchange');
1655 History.busy(false);
1658 // We don't care for hashes
1659 History.expectedStateId = false;
1664 stateId = History.Adapter.extractEventData('state',event,extra) || false;
1668 // Vanilla: Back/forward button was used
1669 newState = History.getStateById(stateId);
1671 else if ( History.expectedStateId ) {
1672 // Vanilla: A new state was pushed, and popstate was called manually
1673 newState = History.getStateById(History.expectedStateId);
1677 newState = History.extractState(History.getLocationHref());
1680 // The State did not exist in our store
1682 // Regenerate the State
1683 newState = History.createStateObject(null,null,History.getLocationHref());
1687 History.expectedStateId = false;
1689 // Check if we are the same state
1690 if ( History.isLastSavedState(newState) ) {
1691 // There has been no change (just the page's hash has finally propagated)
1692 //History.debug('History.onPopState: no change', newState, History.savedStates);
1693 History.busy(false);
1698 History.storeState(newState);
1699 History.saveState(newState);
1701 // Force update of the title
1702 History.setTitle(newState);
1705 History.Adapter.trigger(window,'statechange');
1706 History.busy(false);
1711 History.Adapter.bind(window,'popstate',History.onPopState);
1714 * History.pushState(data,title,url)
1715 * Add a new State to the history object, become it, and trigger onpopstate
1716 * We have to trigger for HTML4 compatibility
1717 * @param {object} data
1718 * @param {string} title
1719 * @param {string} url
1722 History.pushState = function(data,title,url,queue){
1723 //History.debug('History.pushState: called', arguments);
1726 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1727 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1731 if ( queue !== false && History.busy() ) {
1732 // Wait + Push to Queue
1733 //History.debug('History.pushState: we must wait', arguments);
1736 callback: History.pushState,
1743 // Make Busy + Continue
1746 // Create the newState
1747 var newState = History.createStateObject(data,title,url);
1750 if ( History.isLastSavedState(newState) ) {
1751 // Won't be a change
1752 History.busy(false);
1755 // Store the newState
1756 History.storeState(newState);
1757 History.expectedStateId = newState.id;
1759 // Push the newState
1760 history.pushState(newState.id,newState.title,newState.url);
1763 History.Adapter.trigger(window,'popstate');
1766 // End pushState closure
1771 * History.replaceState(data,title,url)
1772 * Replace the State and trigger onpopstate
1773 * We have to trigger for HTML4 compatibility
1774 * @param {object} data
1775 * @param {string} title
1776 * @param {string} url
1779 History.replaceState = function(data,title,url,queue){
1780 //History.debug('History.replaceState: called', arguments);
1783 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1784 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1788 if ( queue !== false && History.busy() ) {
1789 // Wait + Push to Queue
1790 //History.debug('History.replaceState: we must wait', arguments);
1793 callback: History.replaceState,
1800 // Make Busy + Continue
1803 // Create the newState
1804 var newState = History.createStateObject(data,title,url);
1807 if ( History.isLastSavedState(newState) ) {
1808 // Won't be a change
1809 History.busy(false);
1812 // Store the newState
1813 History.storeState(newState);
1814 History.expectedStateId = newState.id;
1816 // Push the newState
1817 history.replaceState(newState.id,newState.title,newState.url);
1820 History.Adapter.trigger(window,'popstate');
1823 // End replaceState closure
1827 } // !History.emulated.pushState
1830 // ====================================================================
1836 if ( sessionStorage ) {
1839 History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
1846 History.normalizeStore();
1851 History.normalizeStore();
1855 * Clear Intervals on exit to prevent memory leaks
1857 History.Adapter.bind(window,"unload",History.clearAllIntervals);
1860 * Create the initial State
1862 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
1865 * Bind for Saving Store
1867 if ( sessionStorage ) {
1868 // When the page is closed
1869 History.onUnload = function(){
1871 var currentStore, item, currentStoreString;
1875 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1882 currentStore.idToState = currentStore.idToState || {};
1883 currentStore.urlToId = currentStore.urlToId || {};
1884 currentStore.stateToId = currentStore.stateToId || {};
1887 for ( item in History.idToState ) {
1888 if ( !History.idToState.hasOwnProperty(item) ) {
1891 currentStore.idToState[item] = History.idToState[item];
1893 for ( item in History.urlToId ) {
1894 if ( !History.urlToId.hasOwnProperty(item) ) {
1897 currentStore.urlToId[item] = History.urlToId[item];
1899 for ( item in History.stateToId ) {
1900 if ( !History.stateToId.hasOwnProperty(item) ) {
1903 currentStore.stateToId[item] = History.stateToId[item];
1907 History.store = currentStore;
1908 History.normalizeStore();
1910 // In Safari, going into Private Browsing mode causes the
1911 // Session Storage object to still exist but if you try and use
1912 // or set any property/function of it it throws the exception
1913 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1914 // add something to storage that exceeded the quota." infinitely
1916 currentStoreString = JSON.stringify(currentStore);
1919 sessionStorage.setItem('History.store', currentStoreString);
1922 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1923 if (sessionStorage.length) {
1924 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1925 // removing/resetting the storage can work.
1926 sessionStorage.removeItem('History.store');
1927 sessionStorage.setItem('History.store', currentStoreString);
1929 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1937 // For Internet Explorer
1938 History.intervalList.push(setInterval(History.onUnload,History.options.storeInterval));
1940 // For Other Browsers
1941 History.Adapter.bind(window,'beforeunload',History.onUnload);
1942 History.Adapter.bind(window,'unload',History.onUnload);
1944 // Both are enabled for consistency
1947 // Non-Native pushState Implementation
1948 if ( !History.emulated.pushState ) {
1949 // Be aware, the following is only for native pushState implementations
1950 // If you are wanting to include something for all browsers
1951 // Then include it above this if block
1956 if ( History.bugs.safariPoll ) {
1957 History.intervalList.push(setInterval(History.safariStatePoll, History.options.safariPollInterval));
1961 * Ensure Cross Browser Compatibility
1963 if ( navigator.vendor === 'Apple Computer, Inc.' || (navigator.appCodeName||'') === 'Mozilla' ) {
1965 * Fix Safari HashChange Issue
1969 History.Adapter.bind(window,'hashchange',function(){
1970 History.Adapter.trigger(window,'popstate');
1974 if ( History.getHash() ) {
1975 History.Adapter.onDomLoad(function(){
1976 History.Adapter.trigger(window,'hashchange');
1981 } // !History.emulated.pushState
1984 }; // History.initCore
1986 // Try to Initialise History
1987 if (!History.options || !History.options.delayInit) {