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 * The list of queues to use
158 * First In, First Out
164 // Initialise History
165 init : function(options){
167 initialTitle : window.document.title,
172 this.storedStates=[];
176 Roo.apply(this,options)
178 // Check Load Status of Adapter
179 //if ( typeof this.Adapter === 'undefined' ) {
183 // Check Load Status of Core
184 if ( typeof this.initCore !== 'undefined' ) {
188 // Check Load Status of HTML4 Support
189 if ( typeof this.initHtml4 !== 'undefined' ) {
196 * Is History enabled?
198 this.enabled = !this.emulated.pushState;
211 // ========================================================================
215 initCore : function(options){
217 this.intervalList = [];
221 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
222 this.sessionStorage.setItem('TEST', '1');
223 this.sessionStorage.removeItem('TEST');
225 this.sessionStorage = false;
229 if ( typeof this.initCore.initialized !== 'undefined' ) {
234 this.initCore.initialized = true;
242 * History.clearAllIntervals
243 * Clears all setInterval instances.
245 clearAllIntervals: function()
247 var i, il = this.intervalList;
248 if (typeof il !== "undefined" && il !== null) {
249 for (i = 0; i < il.length; i++) {
250 clearInterval(il[i]);
252 this.intervalList = null;
257 // ====================================================================
261 * History.debugLog(message,...)
262 * Logs the passed arguments if debug enabled
264 debugLog : function()
266 if ( (this.debug||false) ) {
267 Roo.log.apply(History,arguments);
273 // ====================================================================
277 * History.getInternetExplorerMajorVersion()
278 * Get's the major version of Internet Explorer
280 * @license Public Domain
281 * @author Benjamin Arthur Lupton <contact@balupton.com>
282 * @author James Padolsey <https://gist.github.com/527683>
284 getInternetExplorerMajorVersion : function(){
285 var result = this.getInternetExplorerMajorVersion.cached =
286 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
287 ? this.getInternetExplorerMajorVersion.cached
290 div = window.document.createElement('div'),
291 all = div.getElementsByTagName('i');
292 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
293 return (v > 4) ? v : false;
300 * isInternetExplorer()
301 * Are we using Internet Explorer?
303 * @license Public Domain
304 * @author Benjamin Arthur Lupton <contact@balupton.com>
306 isInternetExplorer : function(){
308 this.isInternetExplorer.cached =
309 (typeof this.isInternetExplorer.cached !== 'undefined')
310 ? this.isInternetExplorer.cached
311 : Boolean(this.getInternetExplorerMajorVersion())
317 * Which features require emulating?
325 initEmulated : function()
329 if (this.html4Mode) {
335 this.emulated.pushState = !Boolean(
336 window.history && window.history.pushState && window.history.replaceState
338 (/ 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) */
339 || (/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 */
342 this.emulated.hashChange = Boolean(
343 !(('onhashchange' in window) || ('onhashchange' in window.document))
345 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
354 * Checks to see if the Object is Empty
355 * @param {Object} obj
358 isEmptyObject = function(obj) {
359 for ( var name in obj ) {
360 if ( obj.hasOwnProperty(name) ) {
369 * Clones a object and eliminate all references to the original contexts
370 * @param {Object} obj
373 cloneObject = function(obj) {
376 hash = JSON.stringify(obj);
377 newObj = JSON.parse(hash);
386 // ====================================================================
391 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
392 * @return {String} rootUrl
394 getRootUrl = function(){
396 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
397 if ( window.document.location.port||false ) {
398 rootUrl += ':'+window.document.location.port;
408 * Fetches the `href` attribute of the `<base href="...">` element if it exists
409 * @return {String} baseHref
411 getBaseHref = function(){
414 baseElements = window.document.getElementsByTagName('base'),
418 // Test for Base Element
419 if ( baseElements.length === 1 ) {
420 // Prepare for Base Element
421 baseElement = baseElements[0];
422 baseHref = baseElement.href.replace(/[^\/]+$/,'');
425 // Adjust trailing slash
426 baseHref = baseHref.replace(/\/+$/,'');
427 if ( baseHref ) baseHref += '/';
435 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
436 * @return {String} baseUrl
438 getBaseUrl = function(){
440 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
448 * Fetches the URL of the current page
449 * @return {String} pageUrl
451 getPageUrl = function(){
454 State = this.getState(false,false),
455 stateUrl = (State||{}).url||this.getLocationHref(),
459 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
460 return (/\./).test(part) ? part : part+'/';
469 * Fetches the Url of the directory of the current page
470 * @return {String} basePageUrl
472 getBasePageUrl = function(){
474 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
475 return (/[^\/]$/).test(part) ? '' : part;
476 }).replace(/\/+$/,'')+'/';
484 * Ensures that we have an absolute URL and not a relative URL
485 * @param {string} url
486 * @param {Boolean} allowBaseHref
487 * @return {string} fullUrl
489 getFullUrl = function(url,allowBaseHref){
491 var fullUrl = url, firstChar = url.substring(0,1);
492 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
495 if ( /[a-z]+\:\/\//.test(url) ) {
498 else if ( firstChar === '/' ) {
500 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
502 else if ( firstChar === '#' ) {
504 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
506 else if ( firstChar === '?' ) {
508 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
512 if ( allowBaseHref ) {
513 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
515 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
517 // We have an if condition above as we do not want hashes
518 // which are relative to the baseHref in our URLs
519 // as if the baseHref changes, then all our bookmarks
520 // would now point to different locations
521 // whereas the basePageUrl will always stay the same
525 return fullUrl.replace(/\#$/,'');
530 * Ensures that we have a relative URL and not a absolute URL
531 * @param {string} url
532 * @return {string} url
534 getShortUrl = function(url){
536 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
539 if ( this.emulated.pushState ) {
540 // We are in a if statement as when pushState is not emulated
541 // The actual url these short urls are relative to can change
542 // So within the same session, we the url may end up somewhere different
543 shortUrl = shortUrl.replace(baseUrl,'');
547 shortUrl = shortUrl.replace(rootUrl,'/');
549 // Ensure we can still detect it as a state
550 if ( this.isTraditionalAnchor(shortUrl) ) {
551 shortUrl = './'+shortUrl;
555 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
562 * getLocationHref(document)
563 * Returns a normalized version of document.location.href
564 * accounting for browser inconsistencies, etc.
566 * This URL will be URI-encoded and will include the hash
568 * @param {object} document
569 * @return {string} url
571 getLocationHref = function(doc) {
572 doc = doc || window.document;
574 // most of the time, this will be true
575 if (doc.URL === doc.location.href)
576 return doc.location.href;
578 // some versions of webkit URI-decode document.location.href
579 // but they leave document.URL in an encoded state
580 if (doc.location.href === decodeURIComponent(doc.URL))
583 // FF 3.6 only updates document.URL when a page is reloaded
584 // document.location.href is updated correctly
585 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
586 return doc.location.href;
588 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
589 return doc.location.href;
591 return doc.URL || doc.location.href;
598 * Noramlize the store by adding necessary values
600 normalizeStore = function(){
601 this.store.idToState = this.store.idToState||{};
602 this.store.urlToId = this.store.urlToId||{};
603 this.store.stateToId = this.store.stateToId||{};
608 * Get an object containing the data, title and url of the current state
609 * @param {Boolean} friendly
610 * @param {Boolean} create
611 * @return {Object} State
613 getState : function(friendly,create){
615 if ( typeof friendly === 'undefined' ) { friendly = true; }
616 if ( typeof create === 'undefined' ) { create = true; }
619 var State = this.getLastSavedState();
622 if ( !State && create ) {
623 State = this.createStateObject();
628 State = this.cloneObject(State);
629 State.url = this.cleanUrl||State.url;
637 * getIdByState(State)
638 * Gets a ID for a State
639 * @param {State} newState
640 * @return {String} id
642 getIdByState = function(newState){
645 var id = this.extractId(newState.url),
649 // Find ID via State String
650 str = this.getStateString(newState);
651 if ( typeof this.stateToId[str] !== 'undefined' ) {
652 id = this.stateToId[str];
654 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
655 id = this.store.stateToId[str];
660 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
661 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
666 // Apply the new State to the ID
667 this.stateToId[str] = id;
668 this.idToState[id] = newState;
677 * normalizeState(State)
678 * Expands a State Object
679 * @param {object} State
682 normalizeState = function(oldState){
684 var newState, dataNotEmpty;
687 if ( !oldState || (typeof oldState !== 'object') ) {
692 if ( typeof oldState.normalized !== 'undefined' ) {
697 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
701 // ----------------------------------------------------------------
705 newState.normalized = true;
706 newState.title = oldState.title||'';
707 newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
708 newState.hash = this.getShortUrl(newState.url);
709 newState.data = this.cloneObject(oldState.data);
712 newState.id = this.getIdByState(newState);
714 // ----------------------------------------------------------------
717 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
718 newState.url = newState.cleanUrl;
720 // Check to see if we have more than just a url
721 dataNotEmpty = !this.isEmptyObject(newState.data);
724 if ( (newState.title || dataNotEmpty) && this.options.disableSuid !== true ) {
726 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
727 if ( !/\?/.test(newState.hash) ) {
728 newState.hash += '?';
730 newState.hash += '&_suid='+newState.id;
733 // Create the Hashed URL
734 newState.hashedUrl = this.getFullUrl(newState.hash);
736 // ----------------------------------------------------------------
738 // Update the URL if we have a duplicate
739 if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
740 newState.url = newState.hashedUrl;
743 // ----------------------------------------------------------------
750 * createStateObject(data,title,url)
751 * Creates a object based on the data, title and url state params
752 * @param {object} data
753 * @param {string} title
754 * @param {string} url
757 createStateObject = function(data,title,url){
766 State = this.normalizeState(State);
774 * Get a state by it's UID
777 getStateById = function(id){
782 var State = this.idToState[id] || this.store.idToState[id] || undefined;
789 * Get a State's String
790 * @param {State} passedState
792 getStateString = function(passedState){
794 var State, cleanedState, str;
797 State = this.normalizeState(passedState);
802 title: passedState.title,
807 str = JSON.stringify(cleanedState);
815 * @param {State} passedState
816 * @return {String} id
818 getStateId = function(passedState){
823 State = this.normalizeState(passedState);
833 * getHashByState(State)
834 * Creates a Hash for the State Object
835 * @param {State} passedState
836 * @return {String} hash
838 getHashByState = function(passedState){
843 State = this.normalizeState(passedState);
853 * extractId(url_or_hash)
854 * Get a State ID by it's URL or Hash
855 * @param {string} url_or_hash
856 * @return {string} id
858 this.extractId = function ( url_or_hash ) {
860 var id,parts,url, tmp;
864 // If the URL has a #, use the id from before the #
865 if (url_or_hash.indexOf('#') != -1)
867 tmp = url_or_hash.split("#")[0];
874 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
875 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
876 id = parts ? String(parts[2]||'') : '';
883 * isTraditionalAnchor
884 * Checks to see if the url is a traditional anchor or not
885 * @param {String} url_or_hash
888 isTraditionalAnchor = function(url_or_hash){
890 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
893 return isTraditional;
898 * Get a State by it's URL or Hash
899 * @param {String} url_or_hash
900 * @return {State|null}
902 extractState = function(url_or_hash,create){
904 var State = null, id, url;
905 create = create||false;
908 id = this.extractId(url_or_hash);
910 State = this.getStateById(id);
913 // Fetch SUID returned no State
916 url = this.getFullUrl(url_or_hash);
919 id = this.getIdByUrl(url)||false;
921 State = this.getStateById(id);
925 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
926 State = this.createStateObject(null,null,url);
936 * Get a State ID by a State URL
938 getIdByUrl = function(url){
940 var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
947 * getLastSavedState()
948 * Get an object containing the data, title and url of the current state
949 * @return {Object} State
951 getLastSavedState = function(){
952 return this.savedStates[this.savedStates.length-1]||undefined;
956 * getLastStoredState()
957 * Get an object containing the data, title and url of the current state
958 * @return {Object} State
960 getLastStoredState = function(){
961 return this.storedStates[this.storedStates.length-1]||undefined;
966 * Checks if a Url will have a url conflict
967 * @param {Object} newState
968 * @return {Boolean} hasDuplicate
970 hasUrlDuplicate = function(newState) {
972 var hasDuplicate = false,
976 oldState = this.extractState(newState.url);
979 hasDuplicate = oldState && oldState.id !== newState.id;
988 * @param {Object} newState
989 * @return {Object} newState
991 storeState = function(newState){
993 this.urlToId[newState.url] = newState.id;
996 this.storedStates.push(this.cloneObject(newState));
1003 * isLastSavedState(newState)
1004 * Tests to see if the state is the last state
1005 * @param {Object} newState
1006 * @return {boolean} isLast
1008 isLastSavedState = function(newState){
1011 newId, oldState, oldId;
1014 if ( this.savedStates.length ) {
1015 newId = newState.id;
1016 oldState = this.getLastSavedState();
1017 oldId = oldState.id;
1020 isLast = (newId === oldId);
1030 * @param {Object} newState
1031 * @return {boolean} changed
1033 saveState = function(newState){
1035 if ( this.isLastSavedState(newState) ) {
1040 this.savedStates.push(this.cloneObject(newState));
1048 * Gets a state by the index
1049 * @param {integer} index
1052 getStateByIndex = function(index){
1057 if ( typeof index === 'undefined' ) {
1058 // Get the last inserted
1059 State = this.savedStates[this.savedStates.length-1];
1061 else if ( index < 0 ) {
1063 State = this.savedStates[this.savedStates.length+index];
1066 // Get from the beginning
1067 State = this.savedStates[index];
1076 * Gets the current index
1079 getCurrentIndex = function(){
1084 if(this.savedStates.length < 1) {
1088 index = this.savedStates.length-1;
1093 // ====================================================================
1098 * @param {Location=} location
1099 * Gets the current document hash
1100 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1103 getHash = function(doc){
1104 var url = this.getLocationHref(doc),
1106 hash = this.getHashByUrl(url);
1112 * normalize and Unescape a Hash
1113 * @param {String} hash
1116 unescapeHash = function(hash){
1118 var result = this.normalizeHash(hash);
1121 result = decodeURIComponent(result);
1129 * normalize a hash across browsers
1132 normalizeHash = function(hash){
1134 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1142 * Sets the document hash
1143 * @param {string} hash
1146 setHash = function(hash,queue){
1151 if ( queue !== false && this.busy() ) {
1152 // Wait + Push to Queue
1153 //this.debug('this.setHash: we must wait', arguments);
1156 callback: this.setHash,
1164 //this.debug('History.setHash: called',hash);
1166 // Make Busy + Continue
1169 // Check if hash is a state
1170 State = this.extractState(hash,true);
1171 if ( State && !this.emulated.pushState ) {
1172 // Hash is a state so skip the setHash
1173 //this.debug('History.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1176 this.pushState(State.data,State.title,State.url,false);
1178 else if ( this.getHash() !== hash ) {
1179 // Hash is a proper hash, so apply it
1181 // Handle browser bugs
1182 if ( this.bugs.setHash ) {
1183 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1185 // Fetch the base page
1186 pageUrl = this.getPageUrl();
1188 // Safari hash apply
1189 this.pushState(null,null,pageUrl+'#'+hash,false);
1192 // Normal hash apply
1193 window.document.location.hash = hash;
1203 * normalize and Escape a Hash
1206 escapeHash = function(hash){
1208 var result = normalizeHash(hash);
1211 result = window.encodeURIComponent(result);
1214 if ( !this.bugs.hashEscape ) {
1215 // Restore common parts
1217 .replace(/\%21/g,'!')
1218 .replace(/\%26/g,'&')
1219 .replace(/\%3D/g,'=')
1220 .replace(/\%3F/g,'?');
1229 * Extracts the Hash from a URL
1230 * @param {string} url
1231 * @return {string} url
1233 getHashByUrl = function(url){
1235 var hash = String(url)
1236 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1240 hash = this.unescapeHash(hash);
1248 * Applies the title to the document
1249 * @param {State} newState
1252 setTitle = function(newState){
1254 var title = newState.title,
1259 firstState = this.getStateByIndex(0);
1260 if ( firstState && firstState.url === newState.url ) {
1261 title = firstState.title||this.options.initialTitle;
1267 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1269 catch ( Exception ) { }
1270 window.document.title = title;
1277 // ====================================================================
1282 * The list of queues to use
1283 * First In, First Out
1285 History.queues = [];
1288 * History.busy(value)
1289 * @param {boolean} value [optional]
1290 * @return {boolean} busy
1292 History.busy = function(value){
1294 if ( typeof value !== 'undefined' ) {
1295 //History.debug('History.busy: changing ['+(History.busy.flag||false)+'] to ['+(value||false)+']', History.queues.length);
1296 History.busy.flag = value;
1299 else if ( typeof History.busy.flag === 'undefined' ) {
1300 History.busy.flag = false;
1304 if ( !History.busy.flag ) {
1305 // Execute the next item in the queue
1306 window.clearTimeout(History.busy.timeout);
1307 var fireNext = function(){
1309 if ( History.busy.flag ) return;
1310 for ( i=History.queues.length-1; i >= 0; --i ) {
1311 queue = History.queues[i];
1312 if ( queue.length === 0 ) continue;
1313 item = queue.shift();
1314 History.fireQueueItem(item);
1315 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1318 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1322 return History.busy.flag;
1328 History.busy.flag = false;
1331 * History.fireQueueItem(item)
1333 * @param {Object} item
1334 * @return {Mixed} result
1336 History.fireQueueItem = function(item){
1337 return item.callback.apply(item.scope||History,item.args||[]);
1341 * History.pushQueue(callback,args)
1342 * Add an item to the queue
1343 * @param {Object} item [scope,callback,args,queue]
1345 History.pushQueue = function(item){
1346 // Prepare the queue
1347 History.queues[item.queue||0] = History.queues[item.queue||0]||[];
1350 History.queues[item.queue||0].push(item);
1357 * History.queue (item,queue), (func,queue), (func), (item)
1358 * Either firs the item now if not busy, or adds it to the queue
1360 History.queue = function(item,queue){
1362 if ( typeof item === 'function' ) {
1367 if ( typeof queue !== 'undefined' ) {
1372 if ( History.busy() ) {
1373 History.pushQueue(item);
1375 History.fireQueueItem(item);
1383 * History.clearQueue()
1386 History.clearQueue = function(){
1387 History.busy.flag = false;
1388 History.queues = [];
1393 // ====================================================================
1397 * History.stateChanged
1398 * States whether or not the state has changed since the last double check was initialised
1400 History.stateChanged = false;
1403 * History.doubleChecker
1404 * Contains the timeout used for the double checks
1406 History.doubleChecker = false;
1409 * History.doubleCheckComplete()
1410 * Complete a double check
1413 History.doubleCheckComplete = function(){
1415 History.stateChanged = true;
1418 History.doubleCheckClear();
1425 * History.doubleCheckClear()
1426 * Clear a double check
1429 History.doubleCheckClear = function(){
1431 if ( History.doubleChecker ) {
1432 window.clearTimeout(History.doubleChecker);
1433 History.doubleChecker = false;
1441 * History.doubleCheck()
1442 * Create a double check
1445 History.doubleCheck = function(tryAgain){
1447 History.stateChanged = false;
1448 History.doubleCheckClear();
1450 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1451 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1452 if ( History.bugs.ieDoubleCheck ) {
1454 History.doubleChecker = window.setTimeout(
1456 History.doubleCheckClear();
1457 if ( !History.stateChanged ) {
1458 //History.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1464 History.options.doubleCheckInterval
1473 // ====================================================================
1477 * History.safariStatePoll()
1478 * Poll the current state
1481 History.safariStatePoll = function(){
1484 // Get the Last State which has the new URL
1486 urlState = History.extractState(History.getLocationHref()),
1489 // Check for a difference
1490 if ( !History.isLastSavedState(urlState) ) {
1491 newState = urlState;
1497 // Check if we have a state with that url
1500 //History.debug('History.safariStatePoll: new');
1501 newState = History.createStateObject();
1504 // Apply the New State
1505 //History.debug('History.safariStatePoll: trigger');
1506 History.Adapter.trigger(window,'popstate');
1513 // ====================================================================
1517 * History.back(queue)
1518 * Send the browser history back one item
1519 * @param {Integer} queue [optional]
1521 History.back = function(queue){
1522 //History.debug('History.back: called', arguments);
1525 if ( queue !== false && History.busy() ) {
1526 // Wait + Push to Queue
1527 //History.debug('History.back: we must wait', arguments);
1530 callback: History.back,
1537 // Make Busy + Continue
1540 // Fix certain browser bugs that prevent the state from changing
1541 History.doubleCheck(function(){
1542 History.back(false);
1553 * History.forward(queue)
1554 * Send the browser history forward one item
1555 * @param {Integer} queue [optional]
1557 History.forward = function(queue){
1558 //History.debug('History.forward: called', arguments);
1561 if ( queue !== false && History.busy() ) {
1562 // Wait + Push to Queue
1563 //History.debug('History.forward: we must wait', arguments);
1566 callback: History.forward,
1573 // Make Busy + Continue
1576 // Fix certain browser bugs that prevent the state from changing
1577 History.doubleCheck(function(){
1578 History.forward(false);
1584 // End forward closure
1589 * History.go(index,queue)
1590 * Send the browser history back or forward index times
1591 * @param {Integer} queue [optional]
1593 History.go = function(index,queue){
1594 //History.debug('History.go: called', arguments);
1602 for ( i=1; i<=index; ++i ) {
1603 History.forward(queue);
1606 else if ( index < 0 ) {
1608 for ( i=-1; i>=index; --i ) {
1609 History.back(queue);
1613 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1621 // ====================================================================
1622 // HTML5 State Support
1624 // Non-Native pushState Implementation
1625 if ( History.emulated.pushState ) {
1627 * Provide Skeleton for HTML4 Browsers
1631 var emptyFunction = function(){};
1632 History.pushState = History.pushState||emptyFunction;
1633 History.replaceState = History.replaceState||emptyFunction;
1634 } // History.emulated.pushState
1636 // Native pushState Implementation
1639 * Use native HTML5 History API Implementation
1643 * History.onPopState(event,extra)
1644 * Refresh the Current State
1646 History.onPopState = function(event,extra){
1648 var stateId = false, newState = false, currentHash, currentState;
1650 // Reset the double check
1651 History.doubleCheckComplete();
1653 // Check for a Hash, and handle apporiatly
1654 currentHash = History.getHash();
1655 if ( currentHash ) {
1657 currentState = History.extractState(currentHash||History.getLocationHref(),true);
1658 if ( currentState ) {
1659 // We were able to parse it, it must be a State!
1660 // Let's forward to replaceState
1661 //History.debug('History.onPopState: state anchor', currentHash, currentState);
1662 History.replaceState(currentState.data, currentState.title, currentState.url, false);
1665 // Traditional Anchor
1666 //History.debug('History.onPopState: traditional anchor', currentHash);
1667 History.Adapter.trigger(window,'anchorchange');
1668 History.busy(false);
1671 // We don't care for hashes
1672 History.expectedStateId = false;
1677 stateId = History.Adapter.extractEventData('state',event,extra) || false;
1681 // Vanilla: Back/forward button was used
1682 newState = History.getStateById(stateId);
1684 else if ( History.expectedStateId ) {
1685 // Vanilla: A new state was pushed, and popstate was called manually
1686 newState = History.getStateById(History.expectedStateId);
1690 newState = History.extractState(History.getLocationHref());
1693 // The State did not exist in our store
1695 // Regenerate the State
1696 newState = History.createStateObject(null,null,History.getLocationHref());
1700 History.expectedStateId = false;
1702 // Check if we are the same state
1703 if ( History.isLastSavedState(newState) ) {
1704 // There has been no change (just the page's hash has finally propagated)
1705 //History.debug('History.onPopState: no change', newState, History.savedStates);
1706 History.busy(false);
1711 History.storeState(newState);
1712 History.saveState(newState);
1714 // Force update of the title
1715 History.setTitle(newState);
1718 History.Adapter.trigger(window,'statechange');
1719 History.busy(false);
1724 History.Adapter.bind(window,'popstate',History.onPopState);
1727 * History.pushState(data,title,url)
1728 * Add a new State to the history object, become it, and trigger onpopstate
1729 * We have to trigger for HTML4 compatibility
1730 * @param {object} data
1731 * @param {string} title
1732 * @param {string} url
1735 History.pushState = function(data,title,url,queue){
1736 //History.debug('History.pushState: called', arguments);
1739 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1740 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1744 if ( queue !== false && History.busy() ) {
1745 // Wait + Push to Queue
1746 //History.debug('History.pushState: we must wait', arguments);
1749 callback: History.pushState,
1756 // Make Busy + Continue
1759 // Create the newState
1760 var newState = History.createStateObject(data,title,url);
1763 if ( History.isLastSavedState(newState) ) {
1764 // Won't be a change
1765 History.busy(false);
1768 // Store the newState
1769 History.storeState(newState);
1770 History.expectedStateId = newState.id;
1772 // Push the newState
1773 history.pushState(newState.id,newState.title,newState.url);
1776 History.Adapter.trigger(window,'popstate');
1779 // End pushState closure
1784 * History.replaceState(data,title,url)
1785 * Replace the State and trigger onpopstate
1786 * We have to trigger for HTML4 compatibility
1787 * @param {object} data
1788 * @param {string} title
1789 * @param {string} url
1792 History.replaceState = function(data,title,url,queue){
1793 //History.debug('History.replaceState: called', arguments);
1796 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1797 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1801 if ( queue !== false && History.busy() ) {
1802 // Wait + Push to Queue
1803 //History.debug('History.replaceState: we must wait', arguments);
1806 callback: History.replaceState,
1813 // Make Busy + Continue
1816 // Create the newState
1817 var newState = History.createStateObject(data,title,url);
1820 if ( History.isLastSavedState(newState) ) {
1821 // Won't be a change
1822 History.busy(false);
1825 // Store the newState
1826 History.storeState(newState);
1827 History.expectedStateId = newState.id;
1829 // Push the newState
1830 history.replaceState(newState.id,newState.title,newState.url);
1833 History.Adapter.trigger(window,'popstate');
1836 // End replaceState closure
1840 } // !History.emulated.pushState
1843 // ====================================================================
1849 if ( sessionStorage ) {
1852 History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
1859 History.normalizeStore();
1864 History.normalizeStore();
1868 * Clear Intervals on exit to prevent memory leaks
1870 History.Adapter.bind(window,"unload",History.clearAllIntervals);
1873 * Create the initial State
1875 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
1878 * Bind for Saving Store
1880 if ( sessionStorage ) {
1881 // When the page is closed
1882 History.onUnload = function(){
1884 var currentStore, item, currentStoreString;
1888 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1895 currentStore.idToState = currentStore.idToState || {};
1896 currentStore.urlToId = currentStore.urlToId || {};
1897 currentStore.stateToId = currentStore.stateToId || {};
1900 for ( item in History.idToState ) {
1901 if ( !History.idToState.hasOwnProperty(item) ) {
1904 currentStore.idToState[item] = History.idToState[item];
1906 for ( item in History.urlToId ) {
1907 if ( !History.urlToId.hasOwnProperty(item) ) {
1910 currentStore.urlToId[item] = History.urlToId[item];
1912 for ( item in History.stateToId ) {
1913 if ( !History.stateToId.hasOwnProperty(item) ) {
1916 currentStore.stateToId[item] = History.stateToId[item];
1920 History.store = currentStore;
1921 History.normalizeStore();
1923 // In Safari, going into Private Browsing mode causes the
1924 // Session Storage object to still exist but if you try and use
1925 // or set any property/function of it it throws the exception
1926 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1927 // add something to storage that exceeded the quota." infinitely
1929 currentStoreString = JSON.stringify(currentStore);
1932 sessionStorage.setItem('History.store', currentStoreString);
1935 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1936 if (sessionStorage.length) {
1937 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1938 // removing/resetting the storage can work.
1939 sessionStorage.removeItem('History.store');
1940 sessionStorage.setItem('History.store', currentStoreString);
1942 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1950 // For Internet Explorer
1951 History.intervalList.push(setInterval(History.onUnload,History.options.storeInterval));
1953 // For Other Browsers
1954 History.Adapter.bind(window,'beforeunload',History.onUnload);
1955 History.Adapter.bind(window,'unload',History.onUnload);
1957 // Both are enabled for consistency
1960 // Non-Native pushState Implementation
1961 if ( !History.emulated.pushState ) {
1962 // Be aware, the following is only for native pushState implementations
1963 // If you are wanting to include something for all browsers
1964 // Then include it above this if block
1969 if ( History.bugs.safariPoll ) {
1970 History.intervalList.push(setInterval(History.safariStatePoll, History.options.safariPollInterval));
1974 * Ensure Cross Browser Compatibility
1976 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1978 * Fix Safari HashChange Issue
1982 History.Adapter.bind(window,'hashchange',function(){
1983 History.Adapter.trigger(window,'popstate');
1987 if ( History.getHash() ) {
1988 History.Adapter.onDomLoad(function(){
1989 History.Adapter.trigger(window,'hashchange');
1994 } // !History.emulated.pushState
1997 }; // History.initCore
1999 // Try to Initialise History
2000 if (!History.options || !History.options.delayInit) {