Skip to content

Instantly share code, notes, and snippets.

@meteormanaged
Created December 14, 2016 16:30
Show Gist options
  • Save meteormanaged/1ecfe84373b3f53022b65e595d33f619 to your computer and use it in GitHub Desktop.
Save meteormanaged/1ecfe84373b3f53022b65e595d33f619 to your computer and use it in GitHub Desktop.
Push or replace history state for ncm.com
// Function to "update" state by pushing or replacing depending on current state.
function updateState(queryParams) {
//Build other options.
var title = document.title;
var newUrl = window.location.protocol + '//' + window.location.host + window.location.pathname + '?' + queryParams;
// Do we already have a query state that we've pushed? Let's look at window.location.search to find out.
if (window.location.search.length > 1) {
// If so, let's replace it.
window.history.replaceState({}, title, newUrl);
} else {
// If not, we'll make the history addition.
window.history.pushState({}, title, newUrl);
}
}
// When someone makes a change that changes the state (moves a piece, changes settings), we give the new serialized data to the function and execute.
// Query Params to append, gathered up however you may be doing so...
var queryParams = 'fen=rnbqkbnr%2Fpppppppp%2F8%2F8%2F8%2F8%2FPPPPPPPP%2FRNBQKBNR+w+Kq+-+0+1&flipped=false';
updateState(queryParams);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment