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 // ========================================================================
85 sessionStorage : false, // sessionStorage
87 intervalList : false, // array normally.
90 init : function(options){
92 initialTitle : window.document.title,
94 Roo.apply(this,options)
96 // Check Load Status of Adapter
97 //if ( typeof this.Adapter === 'undefined' ) {
101 // Check Load Status of Core
102 if ( typeof this.initCore !== 'undefined' ) {
106 // Check Load Status of HTML4 Support
107 if ( typeof this.initHtml4 !== 'undefined' ) {
116 // ========================================================================
120 initCore = function(options){
122 this.intervalList = [];
124 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
125 this.sessionStorage.setItem('TEST', '1');
126 this.sessionStorage.removeItem('TEST');
128 this.sessionStorage = false;
132 if ( typeof this.initCore.initialized !== 'undefined' ) {
137 this.initCore.initialized = true;
142 // ====================================================================
146 * History.intervalList
147 * List of intervals set, to be cleared when document is unloaded.
152 * History.clearAllIntervals
153 * Clears all setInterval instances.
155 clearAllIntervals = function(){
156 var i, il = History.intervalList;
157 if (typeof il !== "undefined" && il !== null) {
158 for (i = 0; i < il.length; i++) {
159 clearInterval(il[i]);
161 History.intervalList = null;
166 // ====================================================================
170 * History.debug(message,...)
171 * Logs the passed arguments if debug enabled
173 History.debugLog = function(){
174 if ( (this.debug||false) ) {
175 Roo.log.apply(History,arguments);
181 // ====================================================================
185 * History.getInternetExplorerMajorVersion()
186 * Get's the major version of Internet Explorer
188 * @license Public Domain
189 * @author Benjamin Arthur Lupton <contact@balupton.com>
190 * @author James Padolsey <https://gist.github.com/527683>
192 History.getInternetExplorerMajorVersion = function(){
193 var result = History.getInternetExplorerMajorVersion.cached =
194 (typeof History.getInternetExplorerMajorVersion.cached !== 'undefined')
195 ? History.getInternetExplorerMajorVersion.cached
198 div = window.document.createElement('div'),
199 all = div.getElementsByTagName('i');
200 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
201 return (v > 4) ? v : false;
208 * History.isInternetExplorer()
209 * Are we using Internet Explorer?
211 * @license Public Domain
212 * @author Benjamin Arthur Lupton <contact@balupton.com>
214 History.isInternetExplorer = function(){
216 History.isInternetExplorer.cached =
217 (typeof History.isInternetExplorer.cached !== 'undefined')
218 ? History.isInternetExplorer.cached
219 : Boolean(History.getInternetExplorerMajorVersion())
226 * Which features require emulating?
229 if (History.options.html4Mode) {
240 window.history && window.history.pushState && window.history.replaceState
242 (/ 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) */
243 || (/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 */
247 !(('onhashchange' in window) || ('onhashchange' in window.document))
249 (History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8)
256 * Is History enabled?
258 History.enabled = !History.emulated.pushState;
262 * Which bugs are present
266 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
267 * https://bugs.webkit.org/show_bug.cgi?id=56249
269 setHash: Boolean(!History.emulated.pushState && window.navigator.vendor === 'Apple Computer, Inc.' && /AppleWebKit\/5([0-2]|3[0-3])/.test(window.navigator.userAgent)),
272 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
273 * https://bugs.webkit.org/show_bug.cgi?id=42940
275 safariPoll: Boolean(!History.emulated.pushState && window.navigator.vendor === 'Apple Computer, Inc.' && /AppleWebKit\/5([0-2]|3[0-3])/.test(window.navigator.userAgent)),
278 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
280 ieDoubleCheck: Boolean(History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8),
283 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
285 hashEscape: Boolean(History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 7)
289 * History.isEmptyObject(obj)
290 * Checks to see if the Object is Empty
291 * @param {Object} obj
294 History.isEmptyObject = function(obj) {
295 for ( var name in obj ) {
296 if ( obj.hasOwnProperty(name) ) {
304 * History.cloneObject(obj)
305 * Clones a object and eliminate all references to the original contexts
306 * @param {Object} obj
309 History.cloneObject = function(obj) {
312 hash = JSON.stringify(obj);
313 newObj = JSON.parse(hash);
322 // ====================================================================
326 * History.getRootUrl()
327 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
328 * @return {String} rootUrl
330 History.getRootUrl = function(){
332 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
333 if ( window.document.location.port||false ) {
334 rootUrl += ':'+window.document.location.port;
343 * History.getBaseHref()
344 * Fetches the `href` attribute of the `<base href="...">` element if it exists
345 * @return {String} baseHref
347 History.getBaseHref = function(){
350 baseElements = window.document.getElementsByTagName('base'),
354 // Test for Base Element
355 if ( baseElements.length === 1 ) {
356 // Prepare for Base Element
357 baseElement = baseElements[0];
358 baseHref = baseElement.href.replace(/[^\/]+$/,'');
361 // Adjust trailing slash
362 baseHref = baseHref.replace(/\/+$/,'');
363 if ( baseHref ) baseHref += '/';
370 * History.getBaseUrl()
371 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
372 * @return {String} baseUrl
374 History.getBaseUrl = function(){
376 var baseUrl = History.getBaseHref()||History.getBasePageUrl()||History.getRootUrl();
383 * History.getPageUrl()
384 * Fetches the URL of the current page
385 * @return {String} pageUrl
387 History.getPageUrl = function(){
390 State = History.getState(false,false),
391 stateUrl = (State||{}).url||History.getLocationHref(),
395 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
396 return (/\./).test(part) ? part : part+'/';
404 * History.getBasePageUrl()
405 * Fetches the Url of the directory of the current page
406 * @return {String} basePageUrl
408 History.getBasePageUrl = function(){
410 var basePageUrl = (History.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
411 return (/[^\/]$/).test(part) ? '' : part;
412 }).replace(/\/+$/,'')+'/';
419 * History.getFullUrl(url)
420 * Ensures that we have an absolute URL and not a relative URL
421 * @param {string} url
422 * @param {Boolean} allowBaseHref
423 * @return {string} fullUrl
425 History.getFullUrl = function(url,allowBaseHref){
427 var fullUrl = url, firstChar = url.substring(0,1);
428 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
431 if ( /[a-z]+\:\/\//.test(url) ) {
434 else if ( firstChar === '/' ) {
436 fullUrl = History.getRootUrl()+url.replace(/^\/+/,'');
438 else if ( firstChar === '#' ) {
440 fullUrl = History.getPageUrl().replace(/#.*/,'')+url;
442 else if ( firstChar === '?' ) {
444 fullUrl = History.getPageUrl().replace(/[\?#].*/,'')+url;
448 if ( allowBaseHref ) {
449 fullUrl = History.getBaseUrl()+url.replace(/^(\.\/)+/,'');
451 fullUrl = History.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
453 // We have an if condition above as we do not want hashes
454 // which are relative to the baseHref in our URLs
455 // as if the baseHref changes, then all our bookmarks
456 // would now point to different locations
457 // whereas the basePageUrl will always stay the same
461 return fullUrl.replace(/\#$/,'');
465 * History.getShortUrl(url)
466 * Ensures that we have a relative URL and not a absolute URL
467 * @param {string} url
468 * @return {string} url
470 History.getShortUrl = function(url){
472 var shortUrl = url, baseUrl = History.getBaseUrl(), rootUrl = History.getRootUrl();
475 if ( History.emulated.pushState ) {
476 // We are in a if statement as when pushState is not emulated
477 // The actual url these short urls are relative to can change
478 // So within the same session, we the url may end up somewhere different
479 shortUrl = shortUrl.replace(baseUrl,'');
483 shortUrl = shortUrl.replace(rootUrl,'/');
485 // Ensure we can still detect it as a state
486 if ( History.isTraditionalAnchor(shortUrl) ) {
487 shortUrl = './'+shortUrl;
491 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
498 * History.getLocationHref(document)
499 * Returns a normalized version of document.location.href
500 * accounting for browser inconsistencies, etc.
502 * This URL will be URI-encoded and will include the hash
504 * @param {object} document
505 * @return {string} url
507 History.getLocationHref = function(doc) {
508 doc = doc || window.document;
510 // most of the time, this will be true
511 if (doc.URL === doc.location.href)
512 return doc.location.href;
514 // some versions of webkit URI-decode document.location.href
515 // but they leave document.URL in an encoded state
516 if (doc.location.href === decodeURIComponent(doc.URL))
519 // FF 3.6 only updates document.URL when a page is reloaded
520 // document.location.href is updated correctly
521 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
522 return doc.location.href;
524 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
525 return doc.location.href;
527 return doc.URL || doc.location.href;
531 // ====================================================================
536 * The store for all session specific data
542 * 1-1: State ID to State Object
544 History.idToState = History.idToState||{};
548 * 1-1: State String to State ID
550 History.stateToId = History.stateToId||{};
554 * 1-1: State URL to State ID
556 History.urlToId = History.urlToId||{};
559 * History.storedStates
560 * Store the states in an array
562 History.storedStates = History.storedStates||[];
565 * History.savedStates
566 * Saved the states in an array
568 History.savedStates = History.savedStates||[];
571 * History.noramlizeStore()
572 * Noramlize the store by adding necessary values
574 History.normalizeStore = function(){
575 History.store.idToState = History.store.idToState||{};
576 History.store.urlToId = History.store.urlToId||{};
577 History.store.stateToId = History.store.stateToId||{};
582 * Get an object containing the data, title and url of the current state
583 * @param {Boolean} friendly
584 * @param {Boolean} create
585 * @return {Object} State
587 History.getState = function(friendly,create){
589 if ( typeof friendly === 'undefined' ) { friendly = true; }
590 if ( typeof create === 'undefined' ) { create = true; }
593 var State = History.getLastSavedState();
596 if ( !State && create ) {
597 State = History.createStateObject();
602 State = History.cloneObject(State);
603 State.url = State.cleanUrl||State.url;
611 * History.getIdByState(State)
612 * Gets a ID for a State
613 * @param {State} newState
614 * @return {String} id
616 History.getIdByState = function(newState){
619 var id = History.extractId(newState.url),
623 // Find ID via State String
624 str = History.getStateString(newState);
625 if ( typeof History.stateToId[str] !== 'undefined' ) {
626 id = History.stateToId[str];
628 else if ( typeof History.store.stateToId[str] !== 'undefined' ) {
629 id = History.store.stateToId[str];
634 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
635 if ( typeof History.idToState[id] === 'undefined' && typeof History.store.idToState[id] === 'undefined' ) {
640 // Apply the new State to the ID
641 History.stateToId[str] = id;
642 History.idToState[id] = newState;
651 * History.normalizeState(State)
652 * Expands a State Object
653 * @param {object} State
656 History.normalizeState = function(oldState){
658 var newState, dataNotEmpty;
661 if ( !oldState || (typeof oldState !== 'object') ) {
666 if ( typeof oldState.normalized !== 'undefined' ) {
671 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
675 // ----------------------------------------------------------------
679 newState.normalized = true;
680 newState.title = oldState.title||'';
681 newState.url = History.getFullUrl(oldState.url?oldState.url:(History.getLocationHref()));
682 newState.hash = History.getShortUrl(newState.url);
683 newState.data = History.cloneObject(oldState.data);
686 newState.id = History.getIdByState(newState);
688 // ----------------------------------------------------------------
691 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
692 newState.url = newState.cleanUrl;
694 // Check to see if we have more than just a url
695 dataNotEmpty = !History.isEmptyObject(newState.data);
698 if ( (newState.title || dataNotEmpty) && History.options.disableSuid !== true ) {
700 newState.hash = History.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
701 if ( !/\?/.test(newState.hash) ) {
702 newState.hash += '?';
704 newState.hash += '&_suid='+newState.id;
707 // Create the Hashed URL
708 newState.hashedUrl = History.getFullUrl(newState.hash);
710 // ----------------------------------------------------------------
712 // Update the URL if we have a duplicate
713 if ( (History.emulated.pushState || History.bugs.safariPoll) && History.hasUrlDuplicate(newState) ) {
714 newState.url = newState.hashedUrl;
717 // ----------------------------------------------------------------
724 * History.createStateObject(data,title,url)
725 * Creates a object based on the data, title and url state params
726 * @param {object} data
727 * @param {string} title
728 * @param {string} url
731 History.createStateObject = function(data,title,url){
740 State = History.normalizeState(State);
747 * History.getStateById(id)
748 * Get a state by it's UID
751 History.getStateById = function(id){
756 var State = History.idToState[id] || History.store.idToState[id] || undefined;
763 * Get a State's String
764 * @param {State} passedState
766 History.getStateString = function(passedState){
768 var State, cleanedState, str;
771 State = History.normalizeState(passedState);
776 title: passedState.title,
781 str = JSON.stringify(cleanedState);
789 * @param {State} passedState
790 * @return {String} id
792 History.getStateId = function(passedState){
797 State = History.normalizeState(passedState);
807 * History.getHashByState(State)
808 * Creates a Hash for the State Object
809 * @param {State} passedState
810 * @return {String} hash
812 History.getHashByState = function(passedState){
817 State = History.normalizeState(passedState);
827 * History.extractId(url_or_hash)
828 * Get a State ID by it's URL or Hash
829 * @param {string} url_or_hash
830 * @return {string} id
832 History.extractId = function ( url_or_hash ) {
834 var id,parts,url, tmp;
838 // If the URL has a #, use the id from before the #
839 if (url_or_hash.indexOf('#') != -1)
841 tmp = url_or_hash.split("#")[0];
848 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
849 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
850 id = parts ? String(parts[2]||'') : '';
857 * History.isTraditionalAnchor
858 * Checks to see if the url is a traditional anchor or not
859 * @param {String} url_or_hash
862 History.isTraditionalAnchor = function(url_or_hash){
864 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
867 return isTraditional;
871 * History.extractState
872 * Get a State by it's URL or Hash
873 * @param {String} url_or_hash
874 * @return {State|null}
876 History.extractState = function(url_or_hash,create){
878 var State = null, id, url;
879 create = create||false;
882 id = History.extractId(url_or_hash);
884 State = History.getStateById(id);
887 // Fetch SUID returned no State
890 url = History.getFullUrl(url_or_hash);
893 id = History.getIdByUrl(url)||false;
895 State = History.getStateById(id);
899 if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) {
900 State = History.createStateObject(null,null,url);
909 * History.getIdByUrl()
910 * Get a State ID by a State URL
912 History.getIdByUrl = function(url){
914 var id = History.urlToId[url] || History.store.urlToId[url] || undefined;
921 * History.getLastSavedState()
922 * Get an object containing the data, title and url of the current state
923 * @return {Object} State
925 History.getLastSavedState = function(){
926 return History.savedStates[History.savedStates.length-1]||undefined;
930 * History.getLastStoredState()
931 * Get an object containing the data, title and url of the current state
932 * @return {Object} State
934 History.getLastStoredState = function(){
935 return History.storedStates[History.storedStates.length-1]||undefined;
939 * History.hasUrlDuplicate
940 * Checks if a Url will have a url conflict
941 * @param {Object} newState
942 * @return {Boolean} hasDuplicate
944 History.hasUrlDuplicate = function(newState) {
946 var hasDuplicate = false,
950 oldState = History.extractState(newState.url);
953 hasDuplicate = oldState && oldState.id !== newState.id;
962 * @param {Object} newState
963 * @return {Object} newState
965 History.storeState = function(newState){
967 History.urlToId[newState.url] = newState.id;
970 History.storedStates.push(History.cloneObject(newState));
977 * History.isLastSavedState(newState)
978 * Tests to see if the state is the last state
979 * @param {Object} newState
980 * @return {boolean} isLast
982 History.isLastSavedState = function(newState){
985 newId, oldState, oldId;
988 if ( History.savedStates.length ) {
990 oldState = History.getLastSavedState();
994 isLast = (newId === oldId);
1004 * @param {Object} newState
1005 * @return {boolean} changed
1007 History.saveState = function(newState){
1009 if ( History.isLastSavedState(newState) ) {
1014 History.savedStates.push(History.cloneObject(newState));
1021 * History.getStateByIndex()
1022 * Gets a state by the index
1023 * @param {integer} index
1026 History.getStateByIndex = function(index){
1031 if ( typeof index === 'undefined' ) {
1032 // Get the last inserted
1033 State = History.savedStates[History.savedStates.length-1];
1035 else if ( index < 0 ) {
1037 State = History.savedStates[History.savedStates.length+index];
1040 // Get from the beginning
1041 State = History.savedStates[index];
1049 * History.getCurrentIndex()
1050 * Gets the current index
1053 History.getCurrentIndex = function(){
1058 if(History.savedStates.length < 1) {
1062 index = History.savedStates.length-1;
1067 // ====================================================================
1072 * @param {Location=} location
1073 * Gets the current document hash
1074 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1077 History.getHash = function(doc){
1078 var url = History.getLocationHref(doc),
1080 hash = History.getHashByUrl(url);
1085 * History.unescapeHash()
1086 * normalize and Unescape a Hash
1087 * @param {String} hash
1090 History.unescapeHash = function(hash){
1092 var result = History.normalizeHash(hash);
1095 result = decodeURIComponent(result);
1102 * History.normalizeHash()
1103 * normalize a hash across browsers
1106 History.normalizeHash = function(hash){
1108 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1115 * History.setHash(hash)
1116 * Sets the document hash
1117 * @param {string} hash
1120 History.setHash = function(hash,queue){
1125 if ( queue !== false && History.busy() ) {
1126 // Wait + Push to Queue
1127 //History.debug('History.setHash: we must wait', arguments);
1130 callback: History.setHash,
1138 //History.debug('History.setHash: called',hash);
1140 // Make Busy + Continue
1143 // Check if hash is a state
1144 State = History.extractState(hash,true);
1145 if ( State && !History.emulated.pushState ) {
1146 // Hash is a state so skip the setHash
1147 //History.debug('History.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1150 History.pushState(State.data,State.title,State.url,false);
1152 else if ( History.getHash() !== hash ) {
1153 // Hash is a proper hash, so apply it
1155 // Handle browser bugs
1156 if ( History.bugs.setHash ) {
1157 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1159 // Fetch the base page
1160 pageUrl = History.getPageUrl();
1162 // Safari hash apply
1163 History.pushState(null,null,pageUrl+'#'+hash,false);
1166 // Normal hash apply
1167 window.document.location.hash = hash;
1177 * normalize and Escape a Hash
1180 History.escapeHash = function(hash){
1182 var result = History.normalizeHash(hash);
1185 result = window.encodeURIComponent(result);
1188 if ( !History.bugs.hashEscape ) {
1189 // Restore common parts
1191 .replace(/\%21/g,'!')
1192 .replace(/\%26/g,'&')
1193 .replace(/\%3D/g,'=')
1194 .replace(/\%3F/g,'?');
1202 * History.getHashByUrl(url)
1203 * Extracts the Hash from a URL
1204 * @param {string} url
1205 * @return {string} url
1207 History.getHashByUrl = function(url){
1209 var hash = String(url)
1210 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1214 hash = History.unescapeHash(hash);
1221 * History.setTitle(title)
1222 * Applies the title to the document
1223 * @param {State} newState
1226 History.setTitle = function(newState){
1228 var title = newState.title,
1233 firstState = History.getStateByIndex(0);
1234 if ( firstState && firstState.url === newState.url ) {
1235 title = firstState.title||History.options.initialTitle;
1241 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1243 catch ( Exception ) { }
1244 window.document.title = title;
1251 // ====================================================================
1256 * The list of queues to use
1257 * First In, First Out
1259 History.queues = [];
1262 * History.busy(value)
1263 * @param {boolean} value [optional]
1264 * @return {boolean} busy
1266 History.busy = function(value){
1268 if ( typeof value !== 'undefined' ) {
1269 //History.debug('History.busy: changing ['+(History.busy.flag||false)+'] to ['+(value||false)+']', History.queues.length);
1270 History.busy.flag = value;
1273 else if ( typeof History.busy.flag === 'undefined' ) {
1274 History.busy.flag = false;
1278 if ( !History.busy.flag ) {
1279 // Execute the next item in the queue
1280 window.clearTimeout(History.busy.timeout);
1281 var fireNext = function(){
1283 if ( History.busy.flag ) return;
1284 for ( i=History.queues.length-1; i >= 0; --i ) {
1285 queue = History.queues[i];
1286 if ( queue.length === 0 ) continue;
1287 item = queue.shift();
1288 History.fireQueueItem(item);
1289 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1292 History.busy.timeout = window.setTimeout(fireNext,History.options.busyDelay);
1296 return History.busy.flag;
1302 History.busy.flag = false;
1305 * History.fireQueueItem(item)
1307 * @param {Object} item
1308 * @return {Mixed} result
1310 History.fireQueueItem = function(item){
1311 return item.callback.apply(item.scope||History,item.args||[]);
1315 * History.pushQueue(callback,args)
1316 * Add an item to the queue
1317 * @param {Object} item [scope,callback,args,queue]
1319 History.pushQueue = function(item){
1320 // Prepare the queue
1321 History.queues[item.queue||0] = History.queues[item.queue||0]||[];
1324 History.queues[item.queue||0].push(item);
1331 * History.queue (item,queue), (func,queue), (func), (item)
1332 * Either firs the item now if not busy, or adds it to the queue
1334 History.queue = function(item,queue){
1336 if ( typeof item === 'function' ) {
1341 if ( typeof queue !== 'undefined' ) {
1346 if ( History.busy() ) {
1347 History.pushQueue(item);
1349 History.fireQueueItem(item);
1357 * History.clearQueue()
1360 History.clearQueue = function(){
1361 History.busy.flag = false;
1362 History.queues = [];
1367 // ====================================================================
1371 * History.stateChanged
1372 * States whether or not the state has changed since the last double check was initialised
1374 History.stateChanged = false;
1377 * History.doubleChecker
1378 * Contains the timeout used for the double checks
1380 History.doubleChecker = false;
1383 * History.doubleCheckComplete()
1384 * Complete a double check
1387 History.doubleCheckComplete = function(){
1389 History.stateChanged = true;
1392 History.doubleCheckClear();
1399 * History.doubleCheckClear()
1400 * Clear a double check
1403 History.doubleCheckClear = function(){
1405 if ( History.doubleChecker ) {
1406 window.clearTimeout(History.doubleChecker);
1407 History.doubleChecker = false;
1415 * History.doubleCheck()
1416 * Create a double check
1419 History.doubleCheck = function(tryAgain){
1421 History.stateChanged = false;
1422 History.doubleCheckClear();
1424 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1425 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1426 if ( History.bugs.ieDoubleCheck ) {
1428 History.doubleChecker = window.setTimeout(
1430 History.doubleCheckClear();
1431 if ( !History.stateChanged ) {
1432 //History.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1438 History.options.doubleCheckInterval
1447 // ====================================================================
1451 * History.safariStatePoll()
1452 * Poll the current state
1455 History.safariStatePoll = function(){
1458 // Get the Last State which has the new URL
1460 urlState = History.extractState(History.getLocationHref()),
1463 // Check for a difference
1464 if ( !History.isLastSavedState(urlState) ) {
1465 newState = urlState;
1471 // Check if we have a state with that url
1474 //History.debug('History.safariStatePoll: new');
1475 newState = History.createStateObject();
1478 // Apply the New State
1479 //History.debug('History.safariStatePoll: trigger');
1480 History.Adapter.trigger(window,'popstate');
1487 // ====================================================================
1491 * History.back(queue)
1492 * Send the browser history back one item
1493 * @param {Integer} queue [optional]
1495 History.back = function(queue){
1496 //History.debug('History.back: called', arguments);
1499 if ( queue !== false && History.busy() ) {
1500 // Wait + Push to Queue
1501 //History.debug('History.back: we must wait', arguments);
1504 callback: History.back,
1511 // Make Busy + Continue
1514 // Fix certain browser bugs that prevent the state from changing
1515 History.doubleCheck(function(){
1516 History.back(false);
1527 * History.forward(queue)
1528 * Send the browser history forward one item
1529 * @param {Integer} queue [optional]
1531 History.forward = function(queue){
1532 //History.debug('History.forward: called', arguments);
1535 if ( queue !== false && History.busy() ) {
1536 // Wait + Push to Queue
1537 //History.debug('History.forward: we must wait', arguments);
1540 callback: History.forward,
1547 // Make Busy + Continue
1550 // Fix certain browser bugs that prevent the state from changing
1551 History.doubleCheck(function(){
1552 History.forward(false);
1558 // End forward closure
1563 * History.go(index,queue)
1564 * Send the browser history back or forward index times
1565 * @param {Integer} queue [optional]
1567 History.go = function(index,queue){
1568 //History.debug('History.go: called', arguments);
1576 for ( i=1; i<=index; ++i ) {
1577 History.forward(queue);
1580 else if ( index < 0 ) {
1582 for ( i=-1; i>=index; --i ) {
1583 History.back(queue);
1587 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1595 // ====================================================================
1596 // HTML5 State Support
1598 // Non-Native pushState Implementation
1599 if ( History.emulated.pushState ) {
1601 * Provide Skeleton for HTML4 Browsers
1605 var emptyFunction = function(){};
1606 History.pushState = History.pushState||emptyFunction;
1607 History.replaceState = History.replaceState||emptyFunction;
1608 } // History.emulated.pushState
1610 // Native pushState Implementation
1613 * Use native HTML5 History API Implementation
1617 * History.onPopState(event,extra)
1618 * Refresh the Current State
1620 History.onPopState = function(event,extra){
1622 var stateId = false, newState = false, currentHash, currentState;
1624 // Reset the double check
1625 History.doubleCheckComplete();
1627 // Check for a Hash, and handle apporiatly
1628 currentHash = History.getHash();
1629 if ( currentHash ) {
1631 currentState = History.extractState(currentHash||History.getLocationHref(),true);
1632 if ( currentState ) {
1633 // We were able to parse it, it must be a State!
1634 // Let's forward to replaceState
1635 //History.debug('History.onPopState: state anchor', currentHash, currentState);
1636 History.replaceState(currentState.data, currentState.title, currentState.url, false);
1639 // Traditional Anchor
1640 //History.debug('History.onPopState: traditional anchor', currentHash);
1641 History.Adapter.trigger(window,'anchorchange');
1642 History.busy(false);
1645 // We don't care for hashes
1646 History.expectedStateId = false;
1651 stateId = History.Adapter.extractEventData('state',event,extra) || false;
1655 // Vanilla: Back/forward button was used
1656 newState = History.getStateById(stateId);
1658 else if ( History.expectedStateId ) {
1659 // Vanilla: A new state was pushed, and popstate was called manually
1660 newState = History.getStateById(History.expectedStateId);
1664 newState = History.extractState(History.getLocationHref());
1667 // The State did not exist in our store
1669 // Regenerate the State
1670 newState = History.createStateObject(null,null,History.getLocationHref());
1674 History.expectedStateId = false;
1676 // Check if we are the same state
1677 if ( History.isLastSavedState(newState) ) {
1678 // There has been no change (just the page's hash has finally propagated)
1679 //History.debug('History.onPopState: no change', newState, History.savedStates);
1680 History.busy(false);
1685 History.storeState(newState);
1686 History.saveState(newState);
1688 // Force update of the title
1689 History.setTitle(newState);
1692 History.Adapter.trigger(window,'statechange');
1693 History.busy(false);
1698 History.Adapter.bind(window,'popstate',History.onPopState);
1701 * History.pushState(data,title,url)
1702 * Add a new State to the history object, become it, and trigger onpopstate
1703 * We have to trigger for HTML4 compatibility
1704 * @param {object} data
1705 * @param {string} title
1706 * @param {string} url
1709 History.pushState = function(data,title,url,queue){
1710 //History.debug('History.pushState: called', arguments);
1713 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1714 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1718 if ( queue !== false && History.busy() ) {
1719 // Wait + Push to Queue
1720 //History.debug('History.pushState: we must wait', arguments);
1723 callback: History.pushState,
1730 // Make Busy + Continue
1733 // Create the newState
1734 var newState = History.createStateObject(data,title,url);
1737 if ( History.isLastSavedState(newState) ) {
1738 // Won't be a change
1739 History.busy(false);
1742 // Store the newState
1743 History.storeState(newState);
1744 History.expectedStateId = newState.id;
1746 // Push the newState
1747 history.pushState(newState.id,newState.title,newState.url);
1750 History.Adapter.trigger(window,'popstate');
1753 // End pushState closure
1758 * History.replaceState(data,title,url)
1759 * Replace the State and trigger onpopstate
1760 * We have to trigger for HTML4 compatibility
1761 * @param {object} data
1762 * @param {string} title
1763 * @param {string} url
1766 History.replaceState = function(data,title,url,queue){
1767 //History.debug('History.replaceState: called', arguments);
1770 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1771 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1775 if ( queue !== false && History.busy() ) {
1776 // Wait + Push to Queue
1777 //History.debug('History.replaceState: we must wait', arguments);
1780 callback: History.replaceState,
1787 // Make Busy + Continue
1790 // Create the newState
1791 var newState = History.createStateObject(data,title,url);
1794 if ( History.isLastSavedState(newState) ) {
1795 // Won't be a change
1796 History.busy(false);
1799 // Store the newState
1800 History.storeState(newState);
1801 History.expectedStateId = newState.id;
1803 // Push the newState
1804 history.replaceState(newState.id,newState.title,newState.url);
1807 History.Adapter.trigger(window,'popstate');
1810 // End replaceState closure
1814 } // !History.emulated.pushState
1817 // ====================================================================
1823 if ( sessionStorage ) {
1826 History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
1833 History.normalizeStore();
1838 History.normalizeStore();
1842 * Clear Intervals on exit to prevent memory leaks
1844 History.Adapter.bind(window,"unload",History.clearAllIntervals);
1847 * Create the initial State
1849 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
1852 * Bind for Saving Store
1854 if ( sessionStorage ) {
1855 // When the page is closed
1856 History.onUnload = function(){
1858 var currentStore, item, currentStoreString;
1862 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1869 currentStore.idToState = currentStore.idToState || {};
1870 currentStore.urlToId = currentStore.urlToId || {};
1871 currentStore.stateToId = currentStore.stateToId || {};
1874 for ( item in History.idToState ) {
1875 if ( !History.idToState.hasOwnProperty(item) ) {
1878 currentStore.idToState[item] = History.idToState[item];
1880 for ( item in History.urlToId ) {
1881 if ( !History.urlToId.hasOwnProperty(item) ) {
1884 currentStore.urlToId[item] = History.urlToId[item];
1886 for ( item in History.stateToId ) {
1887 if ( !History.stateToId.hasOwnProperty(item) ) {
1890 currentStore.stateToId[item] = History.stateToId[item];
1894 History.store = currentStore;
1895 History.normalizeStore();
1897 // In Safari, going into Private Browsing mode causes the
1898 // Session Storage object to still exist but if you try and use
1899 // or set any property/function of it it throws the exception
1900 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1901 // add something to storage that exceeded the quota." infinitely
1903 currentStoreString = JSON.stringify(currentStore);
1906 sessionStorage.setItem('History.store', currentStoreString);
1909 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1910 if (sessionStorage.length) {
1911 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1912 // removing/resetting the storage can work.
1913 sessionStorage.removeItem('History.store');
1914 sessionStorage.setItem('History.store', currentStoreString);
1916 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1924 // For Internet Explorer
1925 History.intervalList.push(setInterval(History.onUnload,History.options.storeInterval));
1927 // For Other Browsers
1928 History.Adapter.bind(window,'beforeunload',History.onUnload);
1929 History.Adapter.bind(window,'unload',History.onUnload);
1931 // Both are enabled for consistency
1934 // Non-Native pushState Implementation
1935 if ( !History.emulated.pushState ) {
1936 // Be aware, the following is only for native pushState implementations
1937 // If you are wanting to include something for all browsers
1938 // Then include it above this if block
1943 if ( History.bugs.safariPoll ) {
1944 History.intervalList.push(setInterval(History.safariStatePoll, History.options.safariPollInterval));
1948 * Ensure Cross Browser Compatibility
1950 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1952 * Fix Safari HashChange Issue
1956 History.Adapter.bind(window,'hashchange',function(){
1957 History.Adapter.trigger(window,'popstate');
1961 if ( History.getHash() ) {
1962 History.Adapter.onDomLoad(function(){
1963 History.Adapter.trigger(window,'hashchange');
1968 } // !History.emulated.pushState
1971 }; // History.initCore
1973 // Try to Initialise History
1974 if (!History.options || !History.options.delayInit) {