templates/jquery/history.html4.js in compass-jquery-plugin-0.3.2.6 vs templates/jquery/history.html4.js in compass-jquery-plugin-0.3.2.7.nil

- old
+ new

@@ -4,687 +4,594 @@ * @author Benjamin Arthur Lupton <contact@balupton.com> * @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com> * @license New BSD License <http://creativecommons.org/licenses/BSD/> */ -(function(window, undefined) { +(function(window,undefined){ + "use strict"; - // -------------------------------------------------------------------------- - // Initialise + // -------------------------------------------------------------------------- + // Initialise - // History Object - window.History = window.History || {}; - window._History = window._History || {}; + // Localise Globals + var + document = window.document, // Make sure we are using the correct document + History = window.History = window.History||{}; // Public History Object - // Localise Globals - var - document = window.document, // Make sure we are using the correct document - _History = window._History, // Private History Object - History = window.History; // Public History Object + // Check Existence + if ( typeof History.initHtml4 !== 'undefined' ) { + throw new Error('History.js HTML4 Support has already been loaded...'); + } - // Check Existence of History.js - if (typeof History.initHtml4 !== 'undefined') { - throw new Error('History.js HTML4 Support has already been loaded...'); - } + // -------------------------------------------------------------------------- + // Initialise HTML4 Support - // Initialise HTML4 Support - History.initHtml4 = function() { + // Initialise HTML4 Support + History.initHtml4 = function(){ + // Initialise + if ( typeof History.initHtml4.initialized !== 'undefined' ) { + // Already Loaded + return false; + } + else { + History.initHtml4.initialized = true; + } - // ---------------------------------------------------------------------- - // Check Status + // ---------------------------------------------------------------------- + // Hash Storage - if (typeof History.initHtml5 === 'undefined' || typeof History.Adapter === 'undefined') { - return false; - } + /** + * History.savedHashes + * Store the hashes in an array + */ + History.savedHashes = []; - // ---------------------------------------------------------------------- - // Emulated Status + /** + * History.isLastHash(newHash) + * Checks if the hash is the last hash + * @param {string} newHash + * @return {boolean} true + */ + History.isLastHash = function(newHash){ + // Prepare + var oldHash = History.getHashByIndex(); - /** - * _History.getInternetExplorerMajorVersion() - * Get's the major version of Internet Explorer - * @return {integer} - * @license Public Domain - * @author Benjamin Arthur Lupton <contact@balupton.com> - * @author James Padolsey <https://gist.github.com/527683> - */ - _History.getInternetExplorerMajorVersion = function() { - var result = _History.getInternetExplorerMajorVersion.cached = - (typeof _History.getInternetExplorerMajorVersion.cached !== 'undefined') - ? _History.getInternetExplorerMajorVersion.cached - : (function() { - var undef, - v = 3, - div = document.createElement('div'), - all = div.getElementsByTagName('i'); - while ( - div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', - all[0] - ); - return v > 4 ? v : undef; - })() - ; - return result; - }; + // Check + var isLast = newHash === oldHash; - /** - * _History.isInternetExplorer() - * Are we using Internet Explorer? - * @return {boolean} - * @license Public Domain - * @author Benjamin Arthur Lupton <contact@balupton.com> - */ - _History.isInternetExplorer = function() { - var result = _History.isInternetExplorer.cached = - (typeof _History.isInternetExplorer.cached !== 'undefined') - ? _History.isInternetExplorer.cached - : (_History.getInternetExplorerMajorVersion() !== 0) - ; - return result; - }; + // Return isLast + return isLast; + }; - /** - * History.emulated - * Which features require emulating? - */ - History.emulated.hashChange = Boolean( - !('onhashchange' in window || 'onhashchange' in document) - || - (_History.isInternetExplorer() && _History.getInternetExplorerMajorVersion() < 8) - ); + /** + * History.saveHash(newHash) + * Push a Hash + * @param {string} newHash + * @return {boolean} true + */ + History.saveHash = function(newHash){ + // Check Hash + if ( History.isLastHash(newHash) ) { + return false; + } + // Push the Hash + History.savedHashes.push(newHash); - // ---------------------------------------------------------------------- - // Hash Storage + // Return true + return true; + }; - /** - * _History.savedHashes - * Store the hashes in an array - */ - _History.savedHashes = []; + /** + * History.getHashByIndex() + * Gets a hash by the index + * @param {integer} index + * @return {string} + */ + History.getHashByIndex = function(index){ + // Prepare + var hash = null; - /** - * _History.isLastHash(newHash) - * Checks if the hash is the last hash - * @param {string} newHash - * @return {boolean} true - */ - _History.isLastHash = function(newHash) { - // Prepare - var oldHash = _History.getHashByIndex(); + // Handle + if ( typeof index === 'undefined' ) { + // Get the last inserted + hash = History.savedHashes[History.savedHashes.length-1]; + } + else if ( index < 0 ) { + // Get from the end + hash = History.savedHashes[History.savedHashes.length+index]; + } + else { + // Get from the beginning + hash = History.savedHashes[index]; + } - // Check - var isLast = newHash === oldHash; + // Return hash + return hash; + }; - // Return isLast - return isLast; - }; + // ---------------------------------------------------------------------- + // Discarded States - /** - * _History.saveHash(newHash) - * Push a Hash - * @param {string} newHash - * @return {boolean} true - */ - _History.saveHash = function(newHash) { - // Check Hash - if (_History.isLastHash(newHash)) { - return false; - } + /** + * History.discardedHashes + * A hashed array of discarded hashes + */ + History.discardedHashes = {}; - // Push the Hash - _History.savedHashes.push(newHash); + /** + * History.discardedStates + * A hashed array of discarded states + */ + History.discardedStates = {}; - // Return true - return true; - }; + /** + * History.discardState(State) + * Discards the state by ignoring it through History + * @param {object} State + * @return {true} + */ + History.discardState = function(discardedState,forwardState,backState){ + //History.debug('History.discardState', arguments); + // Prepare + var discardedStateHash = History.getHashByState(discardedState); - /** - * _History.getHashByIndex() - * Gets a hash by the index - * @param {integer} index - * @return {string} - */ - _History.getHashByIndex = function(index) { - // Prepare - var hash = null; + // Create Discard Object + var discardObject = { + 'discardedState': discardedState, + 'backState': backState, + 'forwardState': forwardState + }; - // Handle - if (typeof index === 'undefined') { - // Get the last inserted - hash = _History.savedHashes[_History.savedHashes.length - 1]; - } - else if (index < 0) { - // Get from the end - hash = _History.savedHashes[_History.savedHashes.length + index]; - } - else { - // Get from the beginning - hash = _History.savedHashes[index]; - } + // Add to DiscardedStates + History.discardedStates[discardedStateHash] = discardObject; - // Return hash - return hash; - }; + // Return true + return true; + }; - /** - * _History.stateHashExists - * Checks if the State Hash Exists - * @param {string} stateHash - * @return {boolean} exists - */ - _History.stateHashExists = function(stateHash) { - // Prepare - var exists = typeof _History.statesByHash[stateHash] !== 'undefined'; + /** + * History.discardHash(hash) + * Discards the hash by ignoring it through History + * @param {string} hash + * @return {true} + */ + History.discardHash = function(discardedHash,forwardState,backState){ + //History.debug('History.discardState', arguments); + // Create Discard Object + var discardObject = { + 'discardedHash': discardedHash, + 'backState': backState, + 'forwardState': forwardState + }; - // Return exists - return exists; - }; + // Add to discardedHash + History.discardedHashes[discardedHash] = discardObject; - // ---------------------------------------------------------------------- - // Discarded States + // Return true + return true; + }; - /** - * _History.discardedHashes - * A hashed array of discarded hashes - */ - _History.discardedHashes = {}; + /** + * History.discardState(State) + * Checks to see if the state is discarded + * @param {object} State + * @return {bool} + */ + History.discardedState = function(State){ + // Prepare + var StateHash = History.getHashByState(State); - /** - * _History.discardedStates - * A hashed array of discarded states - */ - _History.discardedStates = {}; + // Check + var discarded = History.discardedStates[StateHash]||false; - /** - * _History.discardState(State) - * Discards the state by ignoring it through History - * @param {object} State - * @return {true} - */ - _History.discardState = function(discardedState, forwardState, backState) { - History.debug('History.discardState', this, arguments); - // Prepare - var discardedStateHash = History.contractState(discardedState); + // Return true + return discarded; + }; - // Create Discard Object - var discardObject = { - 'discardedState': discardedState, - 'backState': backState, - 'forwardState': forwardState - }; + /** + * History.discardedHash(hash) + * Checks to see if the state is discarded + * @param {string} State + * @return {bool} + */ + History.discardedHash = function(hash){ + // Check + var discarded = History.discardedHashes[hash]||false; - // Add to DiscardedStates - _History.discardedStates[discardedStateHash] = discardObject; + // Return true + return discarded; + }; - // Return true - return true; - }; + /** + * History.recycleState(State) + * Allows a discarded state to be used again + * @param {object} data + * @param {string} title + * @param {string} url + * @return {true} + */ + History.recycleState = function(State){ + //History.debug('History.recycleState', arguments); + // Prepare + var StateHash = History.getHashByState(State); - /** - * _History.discardHash(hash) - * Discards the hash by ignoring it through History - * @param {string} hash - * @return {true} - */ - _History.discardHash = function(discardedHash, forwardState, backState) { - History.debug('History.discardState', this, arguments); - // Create Discard Object - var discardObject = { - 'discardedHash': discardedHash, - 'backState': backState, - 'forwardState': forwardState - }; + // Remove from DiscardedStates + if ( History.discardedState(State) ) { + delete History.discardedStates[StateHash]; + } - // Add to discardedHash - _History.discardedHashes[discardedHash] = discardObject; + // Return true + return true; + }; - // Return true - return true; - }; + // ---------------------------------------------------------------------- + // HTML4 HashChange Support - /** - * _History.discardState(State) - * Checks to see if the state is discarded - * @param {object} State - * @return {bool} - */ - _History.discardedState = function(State) { - // Prepare - var StateHash = History.contractState(State); + if ( History.emulated.hashChange ) { + /* + * We must emulate the HTML4 HashChange Support by manually checking for hash changes + */ - // Check - var discarded = _History.discardedStates[StateHash] || false; + /** + * History.hashChangeInit() + * Init the HashChange Emulation + */ + History.hashChangeInit = function(){ + // Define our Checker Function + History.checkerFunction = null; - // Return true - return discarded; - }; + // Define some variables that will help in our checker function + var + lastDocumentHash = ''; - /** - * _History.discardedHash(hash) - * Checks to see if the state is discarded - * @param {string} State - * @return {bool} - */ - _History.discardedHash = function(hash) { - // Check - var discarded = _History.discardedHashes[hash] || false; + // Handle depending on the browser + if ( History.isInternetExplorer() ) { + // IE6 and IE7 + // We need to use an iframe to emulate the back and forward buttons - // Return true - return discarded; - }; + // Create iFrame + var + iframeId = 'historyjs-iframe', + iframe = document.createElement('iframe'); - /** - * _History.recycleState(State) - * Allows a discarded state to be used again - * @param {object} data - * @param {string} title - * @param {string} url - * @return {true} - */ - _History.recycleState = function(State) { - History.debug('History.recycleState', this, arguments); - // Prepare - var StateHash = History.contractState(State); + // Adjust iFarme + iframe.setAttribute('id', iframeId); + iframe.style.display = 'none'; - // Remove from DiscardedStates - if (_History.discardedState(State)) { - delete _History.discardedStates[StateHash]; - } + // Append iFrame + document.body.appendChild(iframe); - // Return true - return true; - }; + // Create initial history entry + iframe.contentWindow.document.open(); + iframe.contentWindow.document.close(); - // ---------------------------------------------------------------------- - // HTML4 HashChange Support + // Define some variables that will help in our checker function + var + lastIframeHash = '', + checkerRunning = false; - if (History.emulated.hashChange) { - /* - * We must emulate the HTML4 HashChange Support by manually checking for hash changes - */ + // Define the checker function + History.checkerFunction = function(){ + // Check Running + if ( checkerRunning ) { + return false; + } - History.Adapter.onDomLoad(function() { - // Define our Checker Function - _History.checkerFunction = null; + // Update Running + checkerRunning = true; - // Handle depending on the browser - if (_History.isInternetExplorer()) { - // IE6 and IE7 - // We need to use an iframe to emulate the back and forward buttons + // Fetch + var + documentHash = History.getHash()||'', + iframeHash = History.unescapeHash(iframe.contentWindow.document.location.hash)||''; - // Create iFrame - var - iframeId = 'historyjs-iframe', - iframe = document.createElement('iframe'); + // The Document Hash has changed (application caused) + if ( documentHash !== lastDocumentHash ) { + // Equalise + lastDocumentHash = documentHash; - // Adjust iFarme - iframe.setAttribute('id', iframeId); - iframe.style.display = 'none'; + // Create a history entry in the iframe + if ( iframeHash !== documentHash ) { + //History.debug('hashchange.checker: iframe hash change', 'documentHash (new):', documentHash, 'iframeHash (old):', iframeHash); - // Append iFrame - document.body.appendChild(iframe); + // Equalise + lastIframeHash = iframeHash = documentHash; - // Create initial history entry - iframe.contentWindow.document.open(); - iframe.contentWindow.document.close(); + // Create History Entry + iframe.contentWindow.document.open(); + iframe.contentWindow.document.close(); - // Define some variables that will help in our checker function - var - lastDocumentHash = null, - lastIframeHash = null, - checkerRunning = false; + // Update the iframe's hash + iframe.contentWindow.document.location.hash = History.escapeHash(documentHash); + } - // Define the checker function - _History.checkerFunction = function() { - // Check Running - if (checkerRunning) { - History.debug('hashchange.checker: checker is running'); - return false; - } + // Trigger Hashchange Event + History.Adapter.trigger(window,'hashchange'); + } - // Update Running - checkerRunning = true; + // The iFrame Hash has changed (back button caused) + else if ( iframeHash !== lastIframeHash ) { + //History.debug('hashchange.checker: iframe hash out of sync', 'iframeHash (new):', iframeHash, 'documentHash (old):', documentHash); - // Fetch - var - documentHash = History.getHash(), - iframeHash = _History.unescapeHash(iframe.contentWindow.document.location.hash); + // Equalise + lastIframeHash = iframeHash; - // The Document Hash has changed (application caused) - if (documentHash !== lastDocumentHash) { - // Equalise - lastDocumentHash = documentHash; + // Update the Hash + History.setHash(iframeHash,false); + } - // Create a history entry in the iframe - if (iframeHash !== documentHash) { - History.debug('hashchange.checker: iframe hash change', 'documentHash (new):', documentHash, 'iframeHash (old):', iframeHash); + // Reset Running + checkerRunning = false; - // Equalise - lastIframeHash = iframeHash = documentHash; + // Return true + return true; + }; + } + else { + // We are not IE + // Firefox 1 or 2, Opera - // Create History Entry - iframe.contentWindow.document.open(); - iframe.contentWindow.document.close(); + // Define the checker function + History.checkerFunction = function(){ + // Prepare + var documentHash = History.getHash(); - // Update the iframe's hash - iframe.contentWindow.document.location.hash = _History.escapeHash(documentHash); - } + // The Document Hash has changed (application caused) + if ( documentHash !== lastDocumentHash ) { + // Equalise + lastDocumentHash = documentHash; - // Trigger Hashchange Event - History.Adapter.trigger(window, 'hashchange'); - } + // Trigger Hashchange Event + History.Adapter.trigger(window,'hashchange'); + } - // The iFrame Hash has changed (back button caused) - else if (iframeHash !== lastIframeHash) { - History.debug('hashchange.checker: iframe hash out of sync', 'iframeHash (new):', iframeHash, 'documentHash (old):', documentHash); + // Return true + return true; + }; + } - // Equalise - lastIframeHash = iframeHash; + // Apply the checker function + setInterval(History.checkerFunction, History.options.hashChangeInterval); - // Update the Hash - History.setHash(iframeHash, false); - } + // Done + return true; + }; // History.hashChangeInit - // Reset Running - checkerRunning = false; + // Bind hashChangeInit + History.Adapter.onDomLoad(History.hashChangeInit); - // Return true - return true; - }; - } - else { - // We are not IE - // Firefox 1 or 2, Opera + } // History.emulated.hashChange - // Define some variables that will help in our checker function - var - lastDocumentHash = null; - // Define the checker function - _History.checkerFunction = function() { - // Prepare - var documentHash = History.getHash(); + // ---------------------------------------------------------------------- + // HTML5 State Support - // The Document Hash has changed (application caused) - if (documentHash !== lastDocumentHash) { - // Equalise - lastDocumentHash = documentHash; + if ( History.emulated.pushState ) { + /* + * We must emulate the HTML5 State Management by using HTML4 HashChange + */ - // Trigger Hashchange Event - History.Adapter.trigger(window, 'hashchange'); - } + /** + * History.onHashChange(event) + * Trigger HTML5's window.onpopstate via HTML4 HashChange Support + */ + History.onHashChange = function(event){ + //History.debug('History.onHashChange', arguments); - // Return true - return true; - }; - } + // Prepare + var + currentUrl = ((event && event.newURL) || document.location.href), + currentHash = History.getHashByUrl(currentUrl), + currentState = null, + currentStateHash = null, + currentStateHashExits = null; - // Apply the checker function - setInterval(_History.checkerFunction, History.options.hashChangeCheckerDelay); + // Check if we are the same state + if ( History.isLastHash(currentHash) ) { + // There has been no change (just the page's hash has finally propagated) + //History.debug('History.onHashChange: no change'); + History.busy(false); + return false; + } - // End onDomLoad closure - return true; - }); + // Reset the double check + History.doubleCheckComplete(); - } + // Store our location for use in detecting back/forward direction + History.saveHash(currentHash); - // ---------------------------------------------------------------------- - // HTML5 State Support + // Expand Hash + currentState = History.extractState(currentHash||document.location.href); + if ( !currentState ) { + //History.debug('History.onHashChange: traditional anchor', currentHash); + // Traditional Anchor Hash + History.Adapter.trigger(window,'anchorchange'); + History.busy(false); + return false; + } - if (History.emulated.pushState) { - /* - * We must emulate the HTML5 State Management by using HTML4 HashChange - */ + // Check if we are the same state + if ( History.isLastSavedState(currentState) ) { + //History.debug('History.onHashChange: no change'); + // There has been no change (just the page's hash has finally propagated) + History.busy(false); + return false; + } - /** - * _History.onHashChange(event) - * Trigger HTML5's window.onpopstate via HTML4 HashChange Support - */ - _History.onHashChange = function(event) { - History.debug('_History.onHashChange', this, arguments); - // Prepare - var - currentUrl = (event && event.newURL) || document.location.href; - currentHash = unescape(History.extractHashFromUrl(currentUrl)), - currentState = null, - currentStateHash = null, - currentStateHashExits = null; + // Create the state Hash + currentStateHash = History.getHashByState(currentState); - // Check if we are the same state - if (_History.isLastHash(currentHash)) { - // There has been no change (just the page's hash has finally propagated) - History.debug('_History.onHashChange: no change'); - History.busy(false); - return false; - } + // Check if we are DiscardedState + var discardObject = History.discardedState(currentState); + if ( discardObject ) { + // Ignore this state as it has been discarded and go back to the state before it + if ( History.getHashByIndex(-2) === History.getHashByState(discardObject.forwardState) ) { + // We are going backwards + //History.debug('History.onHashChange: go backwards'); + History.back(false); + } else { + // We are going forwards + //History.debug('History.onHashChange: go forwards'); + History.forward(false); + } + History.busy(false); + return false; + } - // Store our location for use in detecting back/forward direction - _History.saveHash(currentHash); + // Push the new HTML5 State + //History.debug('History.onHashChange: success hashchange'); + History.pushState(currentState.data,currentState.title,currentState.url,false); - // Expand Hash - currentState = History.expandHash(currentHash); - if (!currentState) { - // Traditional Anchor Hash - History.debug('_History.onHashChange: traditional anchor'); - History.Adapter.trigger(window, 'anchorchange'); - History.busy(false); - return false; - } + // End onHashChange closure + return true; + }; + History.Adapter.bind(window,'hashchange',History.onHashChange); - // Check if we are the same state - if (_History.isLastState(currentState)) { - // There has been no change (just the page's hash has finally propagated) - History.debug('_History.onHashChange: no change'); - History.busy(false); - return false; - } + /** + * History.pushState(data,title,url) + * Add a new State to the history object, become it, and trigger onpopstate + * We have to trigger for HTML4 compatibility + * @param {object} data + * @param {string} title + * @param {string} url + * @return {true} + */ + History.pushState = function(data,title,url,queue){ + //History.debug('History.pushState: called', arguments); - // Create the state Hash - currentStateHash = History.contractState(currentState); + // Check the State + if ( History.getHashByUrl(url) ) { + throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).'); + } - // Log - History.debug('_History.onHashChange: ', - 'currentStateHash', - currentStateHash, - 'Hash -1', - _History.getHashByIndex(-1), - 'Hash -2', - _History.getHashByIndex(-2), - 'Hash -3', - _History.getHashByIndex(-3), - 'Hash -4', - _History.getHashByIndex(-4), - 'Hash -5', - _History.getHashByIndex(-5), - 'Hash -6', - _History.getHashByIndex(-6), - 'Hash -7', - _History.getHashByIndex(-7) - ); + // Handle Queueing + if ( queue !== false && History.busy() ) { + // Wait + Push to Queue + //History.debug('History.pushState: we must wait', arguments); + History.pushQueue({ + scope: History, + callback: History.pushState, + args: arguments, + queue: queue + }); + return false; + } - // Check if we are DiscardedState - var discardObject = _History.discardedState(currentState); - if (discardObject) { - History.debug('forwardState:', History.contractState(discardObject.forwardState), 'backState:', History.contractState(discardObject.backState)); - // Ignore this state as it has been discarded and go back to the state before it - if (_History.getHashByIndex(-2) === History.contractState(discardObject.forwardState)) { - // We are going backwards - History.debug('_History.onHashChange: go backwards'); - History.back(false); - } else { - // We are going forwards - History.debug('_History.onHashChange: go forwards'); - History.forward(false); - } - History.busy(false); - return false; - } + // Make Busy + History.busy(true); - // Push the new HTML5 State - History.debug('_History.onHashChange: success hashchange'); - History.pushState(currentState.data, currentState.title, currentState.url, false); + // Fetch the State Object + var + newState = History.createStateObject(data,title,url), + newStateHash = History.getHashByState(newState), + oldState = History.getState(false), + oldStateHash = History.getHashByState(oldState), + html4Hash = History.getHash(); - // End onHashChange closure - return true; - }; - History.Adapter.bind(window, 'hashchange', _History.onHashChange); + // Store the newState + History.storeState(newState); + History.expectedStateId = newState.id; - /** - * History.pushState(data,title,url) - * Add a new State to the history object, become it, and trigger onpopstate - * We have to trigger for HTML4 compatibility - * @param {object} data - * @param {string} title - * @param {string} url - * @return {true} - */ - History.pushState = function(data, title, url, queue) { - History.debug('History.pushState', this, arguments); + // Recycle the State + History.recycleState(newState); - // Check the State - if (History.extractHashFromUrl(url)) { - throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).'); - } + // Force update of the title + History.setTitle(newState); - // Handle Queueing - if (queue !== false && History.busy()) { - // Wait + Push to Queue - History.debug('History.pushState: we must wait', arguments); - History.pushQueue({ - scope: History, - callback: History.pushState, - args: arguments, - queue: queue - }); - return false; - } + // Check if we are the same State + if ( newStateHash === oldStateHash ) { + //History.debug('History.pushState: no change', newStateHash); + History.busy(false); + return false; + } - // Make Busy - History.busy(true); + // Update HTML4 Hash + if ( newStateHash !== html4Hash && newStateHash !== History.getShortUrl(document.location.href) ) { + //History.debug('History.pushState: update hash', newStateHash, html4Hash); + History.setHash(newStateHash,false); + return false; + } - // Fetch the State Object - var - newState = History.createStateObject(data, title, url), - newStateHash = History.contractState(newState), - oldState = History.getState(), - oldStateHash = History.getStateHash(), - html4Hash = unescape(History.getHash()); + // Update HTML5 State + History.saveState(newState); - // Store the newState - _History.storeState(newState); + // Fire HTML5 Event + //History.debug('History.pushState: trigger popstate'); + History.Adapter.trigger(window,'statechange'); + History.busy(false); - // Recycle the State - _History.recycleState(newState); + // End pushState closure + return true; + }; - // Force update of the title - if (document.title !== newState.title) { - document.title = newState.title - try { - document.getElementsByTagName('title')[0].innerHTML = newState.title; - } - catch (Exception) { - } - } + /** + * History.replaceState(data,title,url) + * Replace the State and trigger onpopstate + * We have to trigger for HTML4 compatibility + * @param {object} data + * @param {string} title + * @param {string} url + * @return {true} + */ + History.replaceState = function(data,title,url,queue){ + //History.debug('History.replaceState: called', arguments); - History.debug( - 'History.pushState: details', - 'newStateHash:', newStateHash, - 'oldStateHash:', oldStateHash, - 'html4Hash:', html4Hash - ); + // Check the State + if ( History.getHashByUrl(url) ) { + throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).'); + } - // Check if we are the same State - if (newStateHash === oldStateHash) { - History.debug('History.pushState: no change', newStateHash); - return false; - } + // Handle Queueing + if ( queue !== false && History.busy() ) { + // Wait + Push to Queue + //History.debug('History.replaceState: we must wait', arguments); + History.pushQueue({ + scope: History, + callback: History.replaceState, + args: arguments, + queue: queue + }); + return false; + } - // Update HTML4 Hash - if (newStateHash !== html4Hash) { - History.debug('History.pushState: update hash', newStateHash); - History.setHash(newStateHash, false); - return false; - } + // Make Busy + History.busy(true); - // Update HTML5 State - _History.saveState(newState); + // Fetch the State Objects + var + newState = History.createStateObject(data,title,url), + oldState = History.getState(false), + previousState = History.getStateByIndex(-2); - // Fire HTML5 Event - History.debug('History.pushState: trigger popstate'); - History.Adapter.trigger(window, 'statechange'); - History.busy(false); + // Discard Old State + History.discardState(oldState,newState,previousState); - // End pushState closure - return true; - }; + // Alias to PushState + History.pushState(newState.data,newState.title,newState.url,false); - /** - * History.replaceState(data,title,url) - * Replace the State and trigger onpopstate - * We have to trigger for HTML4 compatibility - * @param {object} data - * @param {string} title - * @param {string} url - * @return {true} - */ - History.replaceState = function(data, title, url, queue) { - History.debug('History.replaceState', this, arguments); - // Check the State - if (History.extractHashFromUrl(url)) { - throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).'); - } + // End replaceState closure + return true; + }; - // Handle Queueing - if (queue !== false && History.busy()) { - // Wait + Push to Queue - History.debug('History.replaceState: we must wait', arguments); - History.pushQueue({ - scope: History, - callback: History.replaceState, - args: arguments, - queue: queue - }); - return false; - } + /** + * Create the initial State + */ + History.saveState(History.storeState(History.createStateObject({},'',document.location.href))); - // Make Busy - History.busy(true); + /** + * Ensure initial state is handled correctly + */ + if ( History.getHash() && !History.emulated.hashChange ) { + History.Adapter.onDomLoad(function(){ + History.Adapter.trigger(window,'hashchange'); + }); + } - // Fetch the State Objects - var - newState = History.createStateObject(data, title, url), - oldState = History.getState(), - previousState = _History.getStateByIndex(-2) + } // History.emulated.pushState - // Discard Old State - _History.discardState(oldState, newState, previousState); + }; // History.initHtml4 - // Alias to PushState - History.pushState(newState.data, newState.title, newState.url, false); - - // End replaceState closure - return true; - }; - - /** - * Ensure initial state is handled correctly - **/ - if (!document.location.hash || document.location.hash === '#') { - History.Adapter.onDomLoad(function() { - History.debug('Internet Explorer Initial State Change Fix'); - var currentState = History.createStateObject({}, '', document.location.href); - History.pushState(currentState.data, currentState.title, currentState.url); - }); - } else if (!History.emulated.hashChange) { - History.debug('Firefox Initial State Change Fix'); - History.Adapter.onDomLoad(function() { - _History.onHashChange(); - }); - } - } - } - - // Try Load HTML4 Support - History.initHtml4(); + // Try and Initialise History + History.init(); })(window);